Template constraint and specializations

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Mar 23 13:27:36 PDT 2012


On 3/23/12, Philippe Sigaud <philippe.sigaud at gmail.com> wrote:
> testFoo is a function that accepts any Foo!( ... ) for any ... The
> second line tests it on a value of type T (T.init).

That can't work. For a Foo!int your code will expand like so:

// original code
void tester(Args...)(Foo!Args args);
tester(T.init);

void tester(Args...)(Foo!Args args);
tester((Foo!int).init);  // T is a template instance

void tester(Foo!int)(Foo!(Foo!int) args);  // Args.. becomes the
template instance type
tester((Foo!int).init);

See for yourself:

template isA(alias Foo)
{
    template isA(T)
    {
        //~ pragma(msg, T.init);
        enum bool isA = __traits(compiles,
        {
            void tester(Args...)(Foo!Args args);
            tester(T.init);
        });
    }
}

struct Foo(T)
{
}

alias isA!Foo isFoo;

void useFoo(T)(T t)
    if (isFoo!T)  // no-go
{
}

void main()
{
    Foo!int foo;
    useFoo(foo);
}


More information about the Digitalmars-d-learn mailing list