Thread overview |
---|
May 20, 2018 Can I infer the type from this? | ||||
---|---|---|---|---|
| ||||
I'd like to pass a symbol as paramater (class static member0 and at same time get the type of this, something like this: template myTemp(alias s) { enum myTemp = templateFunction!(??)(s.stringof); } the templateFunction has this signature: int templateFunction(T)(string targetMembername) but I don't how to get the type name from the give symbol name in myTemp. Can I make this work? |
May 20, 2018 Re: Can I infer the type from this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dr.No | On Sunday, 20 May 2018 at 01:41:03 UTC, Dr.No wrote: > I'd like to pass a symbol as paramater (class static member0 and at same time get the type of this, something like this: > > template myTemp(alias s) > { > enum myTemp = templateFunction!(??)(s.stringof); > } > > the templateFunction has this signature: > > int templateFunction(T)(string targetMembername) > > but I don't how to get the type name from the give symbol name in myTemp. Can I make this work? Something like this? ´´´ import std.stdio; void main() { size_t s; auto res = myTemp!s; } template myTemp(alias s) { enum myTemp = templateFunction!(typeof(s))(s.stringof); } int templateFunction(T)(string targetMembername) { static assert(is(T == size_t)); assert(targetMembername == "s"); return 42; } ´´´ |
May 20, 2018 Re: Can I infer the type from this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Sunday, 20 May 2018 at 02:01:20 UTC, Alex wrote:
> On Sunday, 20 May 2018 at 01:41:03 UTC, Dr.No wrote:
>> I'd like to pass a symbol as paramater (class static member0 and at same time get the type of this, something like this:
>>
>> template myTemp(alias s)
>> {
>> enum myTemp = templateFunction!(??)(s.stringof);
>> }
>>
>> the templateFunction has this signature:
>>
>> int templateFunction(T)(string targetMembername)
>>
>> but I don't how to get the type name from the give symbol name in myTemp. Can I make this work?
>
> Something like this?
>
> ´´´
> import std.stdio;
>
> void main()
> {
> size_t s;
> auto res = myTemp!s;
> }
>
>
> template myTemp(alias s)
> {
> enum myTemp = templateFunction!(typeof(s))(s.stringof);
> }
>
>
> int templateFunction(T)(string targetMembername)
> {
> static assert(is(T == size_t));
> assert(targetMembername == "s");
> return 42;
> }
> ´´´
Oh, my bad: I totally forgot a crucial thing on question: I want this to work with a static member, for example, call myTemp like this myTemp!(C.a) I don't mind if I to pass the type as parameter somehow, like myTemp!(C, C.a) or myTemp!(C)(C.a) but I do need to pass a symbol as parameter, hence I'm using alias template parameter.
|
May 20, 2018 Re: Can I infer the type from this? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dr.No | On Sunday, 20 May 2018 at 03:16:39 UTC, Dr.No wrote: > > Oh, my bad: I totally forgot a crucial thing on question: I want this to work with a static member, for example, call myTemp like this myTemp!(C.a) I don't mind if I to pass the type as parameter somehow, like myTemp!(C, C.a) or myTemp!(C)(C.a) but I do need to pass a symbol as parameter, hence I'm using alias template parameter. Still not sure, if I get you right... ´´´ import std.stdio; void main() { auto res = myTemp!(C.s); } class C { static size_t s; } template myTemp(alias s) { enum myTemp = templateFunction!(typeof(s))(s.stringof); } int templateFunction(T)(string targetMembername) { static assert(is(T == size_t)); assert(targetMembername == "s"); return 42; } ´´´ In general, I'm not sure, how you want to get a type of a general symbol (this is what templateFunction should do, isn't it?) as it could be a template or a value parameter for example. However, I tried these cases, and typeof yields something reasonable then. In particular, it still did compile and yields something which could be checked against... |
Copyright © 1999-2021 by the D Language Foundation