Variadic grouping

bearophile bearophileHUGS at lycos.com
Mon Jul 29 08:31:38 PDT 2013


> How do you tell apart values from not values? :-)

Is this brutal enough? :-)


enum SameKind(T...) =
     __traits(compiles, {enum x = T[0], y = T[1]; }) ||
     __traits(compiles, {alias x = T[0]; alias y = T[1]; });

template Select(bool condition, T...)
if (T.length == 2 && SameKind!T) {
     static if (__traits(compiles, {enum x = T[0];})) {
         static if (condition)
             enum Select = T[0];
         else
             enum Select = T[1];
     } else {
         static if (condition)
             alias Select = T[0];
         else
             alias Select = T[1];
     }
}

void main() {
     enum x = Select!(true, 10, 20);
     static assert(x == 10);
     int a = 1;
     int b = 2;
     alias y = Select!(true, a, b);
     assert(y == 1);
     alias T = Select!(true, int, long);
     static assert(is(T == int));
}

Bye,
bearophile


More information about the Digitalmars-d mailing list