Does __traits (hasMember) work with classes?

According to the documentation , __traits (hasMember, ...) should work with any type that has members, but I can't get the code that uses it to compile classes. Observe the following snippet:

struct A {
    int foo;
}

static assert(__traits(hasMember, A, "foo"));

This compiles (although it will not bind because there is no core). But if I change β€œstruct A” to β€œclass A”, the static statement fires and the code will not compile. I can't find anything in the error tracker, and it looks like it's a big E on the eye chart. Am I doing something wrong?

- Edit: Following the getMember example, it seems that hasMember only works on the class if foo is static. Alternatively, I can create an instance of A and test the instance for non-static foo. The problem is that I need to check for the existence of a non-static member of the class at compile time.

+3
source share
1 answer

, , . , hasMember getMember hasMember , . , - , . , instanceHasMember , , .

  is(typeof(mixin(`thing.`~member)))

.

+1

Source: https://habr.com/ru/post/1790078/


All Articles