| 1 |
// REQUIRED_ARGS: -o- |
| 2 |
// PERMUTE_ARGS: |
| 3 |
|
| 4 |
// enum ident(tpl) = Initializer; |
| 5 |
|
| 6 |
enum isIntegral(T) = is(T == int) || is(T == long); |
| 7 |
static assert( isIntegral!int); |
| 8 |
static assert( isIntegral!long); |
| 9 |
static assert(!isIntegral!double); |
| 10 |
static assert(!isIntegral!(int[])); |
| 11 |
|
| 12 |
version(none) |
| 13 |
{ |
| 14 |
enum |
| 15 |
allSatisfy(alias pred, TL...) = |
| 16 |
TL.length == 0 || (pred!(TL[0]) && allSatisfy!(pred, TL[1..$])), |
| 17 |
anySatisfy(alias pred, TL...) = |
| 18 |
TL.length != 0 && (pred!(TL[0]) || anySatisfy!(pred, TL[1..$])) || false; |
| 19 |
static assert( allSatisfy!(isIntegral, int, long)); |
| 20 |
static assert(!allSatisfy!(isIntegral, int, double)); |
| 21 |
static assert( anySatisfy!(isIntegral, int, double)); |
| 22 |
static assert(!anySatisfy!(isIntegral, int[], double)); |
| 23 |
} |
| 24 |
|
| 25 |
void test1() |
| 26 |
{ |
| 27 |
// statement |
| 28 |
enum isIntegral2(T) = is(T == int) || is(T == long); |
| 29 |
static assert(isIntegral2!int); |
| 30 |
} |
| 31 |
|
| 32 |
/******************************************/ |
| 33 |
// alias ident(tpl) = Type; |
| 34 |
|
| 35 |
alias TypeTuple(TL...) = TL; |
| 36 |
static assert(is(TypeTuple!(int, long)[0] == int)); |
| 37 |
static assert(is(TypeTuple!(int, long)[1] == long)); |
| 38 |
|
| 39 |
alias Id(T) = T, Id(alias A) = A; |
| 40 |
static assert(is(Id!int == int)); |
| 41 |
static assert(__traits(isSame, Id!TypeTuple, TypeTuple)); |
| 42 |
|
| 43 |
void test2() |
| 44 |
{ |
| 45 |
// statement |
| 46 |
alias TypeTuple2(TL...) = TL; |
| 47 |
static assert(is(TypeTuple2!(int, long)[0] == int)); |
| 48 |
static assert(is(TypeTuple2!(int, long)[1] == long)); |
| 49 |
|
| 50 |
alias IdT(T) = T, IdA(alias A) = A; |
| 51 |
static assert(is(IdT!int == int)); |
| 52 |
static assert(__traits(isSame, IdA!TypeTuple, TypeTuple)); |
| 53 |
} |
| 54 |
|
| 55 |
/******************************************/ |
| 56 |
// template auto declaration |
| 57 |
|
| 58 |
auto tynameLen(T) = T.stringof.length; |
| 59 |
|
| 60 |
void test3() |
| 61 |
{ |
| 62 |
assert(tynameLen!int == 3); |
| 63 |
assert(tynameLen!long == 4); |
| 64 |
tynameLen!int = 4; |
| 65 |
tynameLen!long = 5; |
| 66 |
assert(tynameLen!int == 4); |
| 67 |
assert(tynameLen!long == 5); |
| 68 |
|
| 69 |
// statement |
| 70 |
auto tynameLen2(T) = T.stringof.length; |
| 71 |
|
| 72 |
assert(tynameLen2!int == 3); |
| 73 |
assert(tynameLen2!long == 4); |
| 74 |
tynameLen2!int = 4; |
| 75 |
tynameLen2!long = 5; |
| 76 |
assert(tynameLen2!int == 4); |
| 77 |
assert(tynameLen2!long == 5); |
| 78 |
} |
| 79 |
|
| 80 |
/******************************************/ |
| 81 |
// template variable declaration |
| 82 |
|
| 83 |
static T math_pi(T) = cast(T)3.1415; |
| 84 |
|
| 85 |
enum bool isFloatingPoint(T) = is(T == float) || is(T == double); |
| 86 |
static assert( isFloatingPoint!double); |
| 87 |
static assert(!isFloatingPoint!string); |
| 88 |
|
| 89 |
void main() |
| 90 |
{ |
| 91 |
assert(math_pi!int == 3); |
| 92 |
assert(math_pi!double == 3.1415); |
| 93 |
|
| 94 |
enum bool isFloatingPoint2(T) = is(T == float) || is(T == double); |
| 95 |
static assert( isFloatingPoint2!double); |
| 96 |
static assert(!isFloatingPoint2!string); |
| 97 |
} |