__traits(parent) borken on templates

Shachar Shemesh shachar at weka.io
Tue Dec 12 14:49:04 UTC 2017


module foo;

import std.traits;

void bar(T)() {
}

pragma(msg, "id:          ", fullyQualifiedName!(bar));
pragma(msg, "parent:      ", fullyQualifiedName!(__traits(parent, bar)));

pragma(msg, "id:          ", fullyQualifiedName!(bar!int));
pragma(msg, "parent:      ", fullyQualifiedName!(__traits(parent, 
bar!int)));
pragma(msg, "grandparent: ", fullyQualifiedName!(__traits(parent, 
__traits(parent, bar!int))));


Compile, and the result is:
id:          foo.bar
parent:      foo
id:          foo.bar!(int)
parent:      foo.bar!(int)
grandparent: foo.bar!(int)

Once the object you have is an inside template, you cannot get its 
parent. I suspect the reason is that bar is actually defined as:

template bar(T) {
   void bar() {
   }
}

So asking for the function's parent supposedly returns the template. 
This doesn't live up to scrutiny, though, because the template's parent 
is the module, but asking for the parent of the parent still returns the 
function.


More information about the Digitalmars-d mailing list