Search code examples
c++static-members

Extract static member type from class of local variable


Is it possible to extract a static member type from the class of a local variable? aka something in the lines of

class A {
 public:
  typedef int constituent_type;
  constituent_type a;
  A(constituent_type _a) :a(_a) {};
}


int main() {
  auto a = A(42);

  // ... lots ... of other code; where I have long since forgottten, what the type of a really was

  std::max<a::constituent_type>(a, a); //<<<
}



Solution

  • You can do that in C++ 11 or later using decltype:

    decltype(a)::x
    

    Live demo:

    https://godbolt.org/z/cTq9zhKxe