Are templates with variadic value parameters possible?

Devin Hill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 15 08:04:22 PDT 2016


On Friday, 15 July 2016 at 05:23:15 UTC, Basile B. wrote:
>
> even better:
>
> template sameType(T...)
> {
>     import std.meta;
>     static if (!T.length)
>         enum sameType = false;
>     else
>         enum sameType = NoDuplicates!T.length == 1;
> }

Yeah, that's basically what I ended up doing, but since I also 
needed to constrain the type of T, I added

is(T[0] : long)

to the condition. It works pretty well! Granted, it doesn't allow 
for calling it in two ways like a variadic version would have:

foo(1, 2, 3)   // works with this setup
foo([1, 2, 3]) // doesn't, but would only be occasionally useful 
anyway

but all in all it's a decent workaround for the problem.


More information about the Digitalmars-d-learn mailing list