Any way to tell if an object is inside another class?

k2aj krzysztof.jajesnica at gmail.com
Mon Sep 28 11:40:40 UTC 2020


On Monday, 28 September 2020 at 11:11:13 UTC, Ruby The Roobster 
wrote:
> For example:
>
> class test {}
> class T {
> auto c = new test();
> }
>
> Any way to tell if an object of type test is a member of object 
> T? I don't want to use the name of the member variable. I just 
> want to know if this works in general.
> Why am I asking this? Because I need it to develop this 
> Multiple Alias This project I am working on(basically just 
> mashing all the functions into a class and then using the class 
> with alias this)

You can use FieldTypeTuple!T to get a compile time sequence of 
T's field types, then use anySatisfy to check whether the field 
types contain the type you want:

import std.traits : FieldTypeTuple;
import std.meta : anySatisfy;

template typeEquals(T) {
     enum typeEquals(U) = is(T == U);
}

enum hasFieldOfType(Obj, Type) = anySatisfy!(
     typeEquals!Type,
     FieldTypeTuple!Obj
);

pragma(msg, hasFieldOfType!(T, int));  //false
pragma(msg, hasFieldOfType!(T, test)); //true


More information about the Digitalmars-d-learn mailing list