0

I would like to check if a type T has a static member variable name: T::name. I saw a lot of solutions (ex: https://stackoverflow.com/a/16000226/5317819, https://stackoverflow.com/a/257382/5317819), but they all require to define first a template struct somewhere before the check, while knowing a priori what is the name of the member.

What I would like is something very 'lightweight' so that inside any function I can do:

if constexpr(HAS_MEMBER_VARIABLE(T,name)) {
    // T::name exists
}

Existing solutions all require to define a template struct such as

template <typename T, typename = int>
struct Has_name : std::false_type { };

template <typename T>
struct Has_name <T, decltype((void) T::name, 0)> : std::true_type { };

#define HAS_MEMBER_VARIABLE(TYPE,NAME) Has_name<TYPE>::type 

How to achieve this without knowing a priori what is the NAME?

Moreover, if in my code I have to check if a type has a member feature_1, feature_2, ..., feature_N, I have to declare first N struct, and I have to know every feature_i in advance.

To simplify the issue, we can make the hypothesis that the static member variable is of type bool if it exists. I am also open for using macros. But the solution needs to be usable at compile time.

T.L
  • 595
  • 3
  • 16

0 Answers0