View source code
Display the source code in std/traits.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
std.traits.Select/select
- multiple declarations
Function select
Select one of two functions to run via template parameter.
A select(bool cond : true, A, B)
(
A a,
lazy B b
);
B select(bool cond : false, A, B)
(
lazy A a,
B b
);
Parameters
Name | Description |
---|---|
cond | A bool which determines which function is run |
a | The first function |
b | The second function |
Returns
a
without evaluating b
if cond
is true
.
Otherwise, returns b
without evaluating a
.
Example
real run() { return 0; }
int fail() { assert(0); }
auto a = select!true(run(), fail());
auto b = select!false(fail(), run());
static assert(is(typeof(a) == real));
static assert(is(typeof(b) == real));
Alias Select
Aliases itself to T[0]
if the boolean condition
is true
and to T[1]
otherwise.
Example
// can select types
static assert(is(Select!(true, int, long) == int));
static assert(is(Select!(false, int, long) == long));
static struct Foo {}
static assert(is(Select!(false, const(int), const(Foo)) == const(Foo)));
// can select symbols
int a = 1;
int b = 2;
alias selA = Select!(true, a, b);
alias selB = Select!(false, a, b);
writeln(selA); // 1
writeln(selB); // 2
// can select (compile-time) expressions
enum val = Select!(false, -4, 9 - 6);
static assert(val == 3);
}
/**
Select one of two functions to run via template parameter.
Params:
cond = A `bool` which determines which function is run
a = The first function
b = The second function
Returns:
`a` without evaluating `b` if `cond` is `true`.
Otherwise, returns `b` without evaluating `a`.
*/
A select(bool cond : true, A, B)(A a, lazy B b) { return a;
Authors
Walter Bright,
Tomasz Stachowiak (isExpressions
),
Andrei Alexandrescu,
Shin Fujishiro,
Robert Clipsham,
David Nadlinger,
Kenji Hara,
Shoichi Kato
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.