Determine at compile time whether or not an alias is defined in a struct/class

ktoast k_toast at protonmail.com
Sun Dec 31 15:01:52 UTC 2017


Hello everyone,


I started to learn D a few weeks ago and have been stuck trying 
to solve the following problem :

- determine at compile time whether an alias is defined or not in 
a struct/class.

You'll find my attempt thereafter, something similar to how it's 
done in C++ (using the type void_t and partial specialization of 
a templated struct) :



alias void_t(T) = void;

struct define_default_type(T) {

     static struct test(U, V = void) {
         static const bool value = false;
     }

     static struct test(U, V : void_t!(U.default_type)) {
         static const bool value = true;
     }

     static const bool value = test!(T).value;
}

unittest {

     struct S {
         alias default_type = void;
     }

    assert(define_default_type!S.value); 
//define_default_type!S.value is false, not sure why
}



I can't tell what's wrong with the code above or if this is even 
the right way to go about it with D.

Would anyone have pointers to give me on how to solve this ?

Thanks a lot !



More information about the Digitalmars-d-learn mailing list