digitalmars.D - Cannot access template name from within template
- Andrei Alexandrescu (10/10) Oct 03 2016 Consider:
- Stefan Koch (5/16) Oct 03 2016 try defining an alias to template name outside of the template
- Andrei Alexandrescu (2/23) Oct 03 2016 Using std.traits.TemplateOf!MyStruct works like a charm. -- Andrei
- Michael Coulombe (5/34) Oct 03 2016 You can also just do something like this, to search the
- Jacob Carlborg (4/6) Oct 03 2016 Doesn't work for nested templates.
- Andrej Mitrovic (41/42) Oct 04 2016 If you're ok with the hacky approach:
- Andrei Alexandrescu (2/3) Oct 04 2016 Have you forgotten you submitted this to phobos? :o) -- Andrei
- Andrej Mitrovic (6/9) Oct 04 2016 I forgot about that template. But that wasn't me:
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
Oct 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, Andreitry defining an alias to template name outside of the template and use that. No guarantees.
Oct 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:Using std.traits.TemplateOf!MyStruct works like a charm. -- AndreiConsider: 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, Andreitry defining an alias to template name outside of the template and use that. No guarantees.
Oct 03 2016
On Tuesday, 4 October 2016 at 00:40:08 UTC, Andrei Alexandrescu wrote:On 10/03/2016 06:32 PM, Stefan Koch wrote:You can also just do something like this, to search the upper/global scope: alias A = SomethingCool!(.MyStruct);On Monday, 3 October 2016 at 22:28:46 UTC, Andrei Alexandrescu wrote:Using std.traits.TemplateOf!MyStruct works like a charm. -- AndreiConsider: 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, Andreitry defining an alias to template name outside of the template and use that. No guarantees.
Oct 03 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
Oct 03 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; } -----
Oct 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
Oct 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: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/e02593090a909948183921f6f00039ce3f37acfftemplate TemplateOf(T)Have you forgotten you submitted this to phobos? :o) -- Andrei
Oct 04 2016