Thread overview
Cannot access template name from within template
Oct 03, 2016
Stefan Koch
Oct 04, 2016
Michael Coulombe
Oct 04, 2016
Jacob Carlborg
Oct 04, 2016
Andrej Mitrovic
Oct 04, 2016
Andrej Mitrovic
October 03, 2016
Consider:

template SomethingCool(alias X) { alias Y = X!int; }

struct MyStruct(T) {
    alias A = SomethingCool!MyStruct;
}

Inside MyStruct though, a mention of the symbol MyStruct alone is actually the current instantiation - i.e. a type, not a template.

Any known workaround?


Thanks,

Andrei
October 03, 2016
On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu wrote:
> Consider:
>
> template SomethingCool(alias X) { alias Y = X!int; }
>
> struct MyStruct(T) {
>     alias A = SomethingCool!MyStruct;
> }
>
> Inside MyStruct though, a mention of the symbol MyStruct alone is actually the current instantiation - i.e. a type, not a template.
>
> Any known workaround?
>
>
> Thanks,
>
> Andrei

try defining an alias to template name outside of the template and use that.
No guarantees.
October 03, 2016
On 10/03/2016 06:32 PM, Stefan Koch wrote:
> On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu wrote:
>> Consider:
>>
>> template SomethingCool(alias X) { alias Y = X!int; }
>>
>> struct MyStruct(T) {
>>     alias A = SomethingCool!MyStruct;
>> }
>>
>> Inside MyStruct though, a mention of the symbol MyStruct alone is
>> actually the current instantiation - i.e. a type, not a template.
>>
>> Any known workaround?
>>
>>
>> Thanks,
>>
>> Andrei
>
> try defining an alias to template name outside of the template and use
> that.
> No guarantees.

Using std.traits.TemplateOf!MyStruct works like a charm. -- Andrei
October 04, 2016
On Tuesday, 4 October 2016 at 00:40:08 UTC, Andrei Alexandrescu wrote:
> On 10/03/2016 06:32 PM, Stefan Koch wrote:
>> On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu wrote:
>>> Consider:
>>>
>>> template SomethingCool(alias X) { alias Y = X!int; }
>>>
>>> struct MyStruct(T) {
>>>     alias A = SomethingCool!MyStruct;
>>> }
>>>
>>> Inside MyStruct though, a mention of the symbol MyStruct alone is
>>> actually the current instantiation - i.e. a type, not a template.
>>>
>>> Any known workaround?
>>>
>>>
>>> Thanks,
>>>
>>> Andrei
>>
>> try defining an alias to template name outside of the template and use
>> that.
>> No guarantees.
>
> Using std.traits.TemplateOf!MyStruct works like a charm. -- Andrei

You can also just do something like this, to search the upper/global scope:

    alias A = SomethingCool!(.MyStruct);
October 04, 2016
On 2016-10-04 04:22, Michael Coulombe wrote:

> You can also just do something like this, to search the upper/global scope:
>
>     alias A = SomethingCool!(.MyStruct);

Doesn't work for nested templates.

-- 
/Jacob Carlborg
October 04, 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;
}
-----
October 04, 2016
On 10/04/2016 06:03 PM, Andrej Mitrovic wrote:
> template TemplateOf(T)

Have you forgotten you submitted this to phobos? :o) -- Andrei
October 04, 2016
On Tuesday, 4 October 2016 at 22:43:21 UTC, Andrei Alexandrescu wrote:
> On 10/04/2016 06:03 PM, Andrej Mitrovic wrote:
>> template TemplateOf(T)
>
> Have you forgotten you submitted this to phobos? :o) -- Andrei

I forgot about that template. But that wasn't me: https://github.com/dlang/phobos/pull/1884 :)

I added isInstanceOf https://github.com/dlang/phobos/commit/e02593090a909948183921f6f00039ce3f37acff