Determine if a type if derived from a template parameter

Alex AJ at gmail.com
Wed Apr 10 23:13:48 UTC 2019


On Wednesday, 10 April 2019 at 19:32:49 UTC, rikki cattermole 
wrote:
> void main()
> {
>     alias Y = X!(int, long);
>
>     static if (is(Y : X!(W, Z), W, Z)) {
>         pragma(msg, W, " ", Z);
>     }
>
> }
>
> class X(T,S)
> {
>    T x;
>    S y;
> }

That is not what I mean. What I mean is to get not the type, but 
the ID for the type from for another id.

class X(T,S)
{
    T x;
    S y;
}

One has x and wants to know x's generic type, which is T in this 
case, not the type T is "holding".

e.g., certain traits returns things that might ultimately be 
derived from using a template parameter. They return the actual 
type though rather than the name.

Your example will return int and long, I actually would want it 
to return T and S, in some sense.

For example, a difficult way to do what I want, but easy in 
principle, would be to read the source code, parse it, and get 
the parameter name from the source.

So, hypothetically:

TemplateNameOf!(X!(int,long).x) == "T"


It would return the template parameter name for anything that was 
defined a template parameter.

Another hypothetical: Suppose one wanted to rebuild the class X 
above, when one reflected to get the members, it would be 
impossible to know which parameter was used for x, if any because 
traits will return, say, int.


It is a similar idea to getting the id's of function parameters. 
[in that we want the symbols used in the source code]


More information about the Digitalmars-d mailing list