Inherit from class based on bool value

Stanislav Blinov stanislav.blinov at gmail.com
Tue Nov 13 10:39:06 UTC 2018


On Tuesday, 13 November 2018 at 07:10:26 UTC, Jamie wrote:

> Is this possible? I can't get it to work in the way I'm showing 
> above.

...or abstract away Ali's solution:

enum OPTION {
     FALSE,
     TRUE,
}

template Select(OPTION opt, IfTrue, IfFalse) {
     static if (opt == OPTION.TRUE) alias Select = IfTrue;
     else alias Select = IfFalse;
}

class One {}
class Two {}

class Top(OPTION opt) : Select!(opt, One, Two) {}

void main() {
     import std.traits : BaseClassesTuple;
     auto t1 = new Top!(OPTION.TRUE);
     static assert(is(BaseClassesTuple!(typeof(t1))[0] == One));
     auto t2 = new Top!(OPTION.FALSE);
     static assert(is(BaseClassesTuple!(typeof(t2))[0] == Two));
}



More information about the Digitalmars-d-learn mailing list