TemplateOf behavior
ag0aep6g
anonymous at example.com
Sat Jan 6 22:25:48 UTC 2018
On Saturday, 6 January 2018 at 19:58:10 UTC, Alex wrote:
> Given the code above, why the static assert inside the mixing
> template constructor yields false, while the others yield true?
Let's resolve the mixin and write out the `T` template in the
verbose form. It looks like this then:
----
template T(alias S)
{
struct T
{
this(int dummy)
{
static assert(__traits(isSame, TemplateOf!(T!arr),
T));
}
}
}
----
Now it's a bit more obvious why the assert fails. At the location
of the assert, `T` refers to the struct, not the template. To
refer to the template, you can prepend a dot, which means that
the lookup is done at the module level instead of searching
upwards from the local level [1]. Like so:
----
static assert(__traits(isSame, TemplateOf!(.T!arr), .T));
----
You don't actually need the dot with `T!arr`. The compiler is
apparently smart enough to figure out that you mean the template
in that case. But it doesn't hurt, either.
[1] https://dlang.org/spec/module.html#module_scope_operators
More information about the Digitalmars-d-learn
mailing list