Help!

Max Samukha maxsamukha at gmail.com
Mon Nov 26 10:20:38 PST 2012


On Monday, 26 November 2012 at 16:37:08 UTC, Manu wrote:
> template isThing( alias symbol, A )
> {
>   enum isThing = false;
> }
>
> This template works in most contexts:
>
> int x;
> struct S {}
>
> pragma(msg, isThing!x);
> pragma(msg, isThing!S);
>
> But this fails:
> pragma(msg, isThing!int);
>
> Why does it fail on a basic type (int), but not a user defined 
> type (S)?
> How can I fix the template declaration to not error in that 
> case?
> I tried:
>
> template isThing( alias symbol, A )
> {
>   enum isThing = false;
> }
> template isThing( T, A )
> {
>   enum isThing = false;
> }
>
> Hoping the T version would catch the int, but this leads to 
> different
> errors "matches more than one template declaration".

That is annoying. There *must* be a kind of single unconstrained 
template parameter accepting anything that can be an element of a 
"compile time" tuple. In other words, this must work for any 
arguments without hassle:

template Foo(alias Head, Tail...)
{
     alias TypeTuple!(Head, Tail) Foo;
}

As others said, you can workaround the deficiency with an 
if-constraint, which is unsightly. FWIW, I have all my codebase 
littered with "if (A.length == 1)" rubbish.



More information about the Digitalmars-d mailing list