How to test templates for equality?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 6 09:04:08 PDT 2014


On 07/05/2014 10:33 PM, Uranuz wrote:
> I have another question about testing if given symbol is instance of the
> given template and geting it's template arguments.

Applying Rene Zwanenburg's message... It is not complete because 
integral template parameters don't work yet.

import std.typetuple;

template instanceArgsOf(alias S, T)
{
     import std.traits : isInstanceOf;

     static if (isInstanceOf!(S, T))
     {
         static if (is(T == S!Args, Args...)) {
             alias instanceArgsOf = Args;

         } else {
             alias instanceArgsOf = void;
         }

     } else {
         alias instanceArgsOf = void;
     }
}

unittest
{
     // Adapting the unittests of std.traits.isInstanceOf

     static struct Foo(T...) { }
     static struct Bar(T...) { }
     static struct Doo(T) { }
     static struct ABC(int x) { }
     static assert(is (instanceArgsOf!(Foo, Foo!(int, double)) ==
                       TypeTuple!(int, double)));
     static assert(is (instanceArgsOf!(Foo, Bar!int) == void));
     static assert(is (instanceArgsOf!(Foo, int) == void));
     static assert(is (instanceArgsOf!(Doo, Doo!int) == TypeTuple!(int)));

     /*
      * The following needs more work because what comes back is something
      * called a 'tuple(1)' (Yes, in lowercase.)
      *
      * static assert(is (instanceArgsOf!(ABC, ABC!1) == TypeTuple!(1)));
      */

     static assert(!__traits(compiles, instanceArgsOf!(Foo, Foo)));
}

void main()
{}

Ali



More information about the Digitalmars-d-learn mailing list