Checking that a template parameter is an enum

Fusxfaranto via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 30 19:06:47 PDT 2015


On Thursday, 1 October 2015 at 00:04:18 UTC, Nordlöw wrote:
> How do I check that a template parameter is a CT-value or an 
> enum symbol?
>
> I want this to restrict the following template:
>
> /** Returns: true iff all values $(D V) are the same. */
> template allSame(V...)          // TODO restrict to values only
> {
>     static if (V.length <= 1)
>         enum bool allSame = true;
>     else
>         enum bool allSame = V[0] == V[1] && allSame!(V[1..$]);
> }
>
> unittest
> {
>     static assert(!allSame!(41, 42));
>     static assert(allSame!(42, 42, 42));
> }

std.traits to the rescue!

http://dlang.org/phobos/std_traits.html#isExpressions

Using isExpressions!V as a template constraint looks like the 
behavior you're looking for.


More information about the Digitalmars-d-learn mailing list