template auto value

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Mar 3 00:20:14 UTC 2018


On Fri, Mar 02, 2018 at 11:51:08PM +0000, Jonathan Marler via Digitalmars-d wrote:
> I believe I found small hole in template parameter semantics.
> [...] you can't create a template that accepts a value of any type.

Not true:

	template counterexample(alias T) {}

	int x;
	string s;
	alias U = counterexample!x;	// OK
	alias V = counterexample!1;	// OK
	alias W = counterexample!"yup";	// OK
	alias X = counterexample!s;	// OK

	alias Z = counterexample!int;	// NG

The last one fails because a value is expected, not a type.

If you *really* want to accept both values and types, `...` comes to the
rescue:

	template rescue(T...) if (T.length == 1) {}

	int x;
	string s;
	alias U = rescue!x;	// OK
	alias V = rescue!1;	// OK
	alias W = rescue!"yup";	// OK
	alias X = rescue!s;	// OK
	alias Z = rescue!int;	// OK!


T

-- 
In a world without fences, who needs Windows and Gates? -- Christian Surchi


More information about the Digitalmars-d mailing list