Variadic parameter of length 1

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 20 08:32:10 PDT 2014


On Wednesday, 20 August 2014 at 15:11:53 UTC, Dominikus Dittes
Scherkl wrote:
> I have several times seen a construct
>
> template foo(T...) if(T.length == 1)
> {
>     ...
> }
>
> What is that good for?
> Why using variadic parameter if anyway exactly one parameter is
> required?!?

That's because template alias parameters cannot take builtin
types like int, and type parameters can only take types. But
tuple elements can be anything.

struct SomeType {}

template a(X) {enum a = 0;} /* type parameter */
enum a1 = a!int; /* ok */
enum a2 = a!SomeType; /* ok */
version(none) enum a3 = a!42; /* Error: template instance a!42
does not match template declaration a(X) */

template b(alias x) {enum b = 0;} /* alias parameter */
version(none) enum b1 = b!int; /* Error: template instance b!int
does not match template declaration b(alias x) */
enum b2 = b!SomeType; /* ok */
enum b3 = b!42; /* ok */

template c(x ...) if(x.length == 1) {enum c = 0;} /* tuple
parameter */
enum c1 = c!int; /* ok */
enum c2 = c!SomeType; /* ok */
enum c3 = c!42; /* ok */


More information about the Digitalmars-d-learn mailing list