Checking that a template parameter is an enum
John Colvin via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Oct 2 01:30:32 PDT 2015
On Friday, 2 October 2015 at 08:13:00 UTC, John Colvin wrote:
> On Thursday, 1 October 2015 at 22:26:39 UTC, Nordlöw wrote:
>> On Thursday, 1 October 2015 at 02:06:48 UTC, Fusxfaranto wrote:
>>> [...]
>>
>> Thanks!
>>
>> BTW: Is there some way to turn the recursive definition of
>> `allSame`
>>
>> template allSame(V...)
>> if (isExpressions!(V))
>> {
>> static if (V.length <= 1)
>> enum allSame = true;
>> else
>> enum allSame = V[0] == V[1] && allSame!(V[1..$]);
>> }
>>
>> into an iterative definition?
>
> Why? To avoid slowing down compilation with all those template
> instantiations?
>
> How about a O(log2(N)) depth recursive version, something like
> this:
>
> template allSame(V ...)
> if (isExpressions!V)
> {
> static if (V.length <= 1)
> enum allSame = true;
> else static if(V.length & 1)
> enum allSame = V[$-1] == V[0]
> && V[0 .. $/2] == V[$/2 .. $-1]
> && allSame!(V[0 .. $/2]);
> else
> enum allSame = V[0..$/2] == V[$/2 .. $]
> && allSame!(V[0 .. $/2]);
> }
Although you should consider that isExpressions is instantiating
V.length templates anyway (it uses a binary split to avoid
excessive template recursion depth, but it ends up checking them
all one-per-template in the end anyway.
More information about the Digitalmars-d-learn
mailing list