Template constraint and specializations

Philippe Sigaud philippe.sigaud at gmail.com
Fri Mar 23 15:02:47 PDT 2012


On Fri, Mar 23, 2012 at 21:27, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:

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

> See for yourself:

? It works for me:


template isBar(T)
{
    enum isBar = __traits(compiles,
                                    {
                                        void tester(Args...)(Bar!Args arg) {}
                                        tester(T.init);
                                    });
}


template isFoo(T)
{
    enum isFoo = __traits(compiles,
                                    {
                                        void tester(Arg)(Foo!Arg arg) {}
                                        tester(T.init);
                                    });
}


struct Foo(T)
{
}

struct Bar(T...)
{}

void useFoo(T)(T t) if (isFoo!T)
{
}

void useBar(T)(T t) if (isBar!T)
{
}

void main()
{
    Foo!int foo;
    useFoo(foo);
    Bar!(int, string, double) bar;
    useBar(bar);
}


More information about the Digitalmars-d-learn mailing list