Fuzzy typedef

I stumbled upon this code and wondered what that means. But even after every 15 minutes, looking at it does not make sense to me.

template< typename T > struct Vector4 { typedef T Vector4<T>::* const vec[4]; static const vec constVec; //just to have some member instances of T T member1, member2, member3, member4; }; 

So what is the type of constVec? Please do not just repeat typedef, but explain in a common language.

My notes:

  • Why are there two types ( T and Vector4<T> ), is this a function pointer?
  • What does it mean: * means? Take everything from the Vector4 area?
  • Is this an array of constant pointers? But why: then?
+6
source share
1 answer

constVec is an array of 4 constant pointers to members of the Vector4<T> class that are of type T

Note. Members are not constants; pointers themselves.

First, since they are constant pointers, you need to initialize them in the constructor: (I just noticed the static classifier, so it must be initialized outside the class, but if it was not static, you need to do this in the initialization list.)

 template< typename T > struct Vector4 { typedef T Vector4<T>::* const vec[4]; static const vec constVec; //just to have some member instances of T T member1, member2, member3, member4; }; template<typename T> const typename Vector4<T>::vec Vector4<T>::constVec = {&Vector4::member1,&Vector4::member2,&Vector4::member3,&Vector4::member4}; int main() { Vector4<int> v; for(int i=0; i<4; i++) { (v.*Vector4<int>::constVec[i]) = 5; } return 0; } 
+10
source

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


All Articles