Cannot access template name from within template
Andrej Mitrovic via Digitalmars-d
digitalmars-d at puremagic.com
Tue Oct 4 15:03:27 PDT 2016
On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu
wrote:
> Any known workaround?
If you're ok with the hacky approach:
-----
import std.algorithm;
import std.conv;
/// these are in std.traits but private for whatever reason
alias Identity(alias A) = A;
alias parentOf(alias sym) = Identity!(__traits(parent, sym));
alias parentOf(alias sym : T!Args, alias T, Args...) =
Identity!(__traits(parent, T));
/// alias itself to the symbol of template instance T
template TemplateOf(T)
{
enum name = T.stringof;
// strip everything after '!' (instantiation args)
enum unqual_name = name.until!(a => a == '!').to!string;
mixin("alias TemplateOf = parentOf!T." ~ unqual_name ~ ";");
}
template SomethingCool(alias X)
{
pragma(msg, X.stringof); // sanity check
alias Y = X!int;
}
struct MyStruct(T)
{
alias A = SomethingCool!(TemplateOf!MyStruct);
}
struct Outer
{
struct Nested(T)
{
alias A = SomethingCool!(TemplateOf!Nested);
}
}
void main ( )
{
alias MyStruct!int X;
alias Outer.Nested!int Y;
}
-----
More information about the Digitalmars-d
mailing list