Please help me understand a code snippet from Facebook Pop: PopVector.h
The template class Vector2
contains a static member _v
which looks like to be the backing data of an instance of Vector2
:
private:
typedef T Vector2<T>::* const _data[2];
static const _data _v;
_v
is instantiated by the following line:
template<typename T>
const typename Vector2<T>::_data Vector2<T>::_v = { &Vector2<T>::x, &Vector2<T>::y };
then _v
is used to implement index operators:
const T& operator[](size_t i) const { return this->*_v[i]; }
T& operator[](size_t i) { return this->*_v[i]; }
I'm not familiar with this code pattern and have several questions about it:
Vector2<T>::*
_v
has to be a static member? It seems it's not shared across instances, which doesn't follow static semantic in C++ AFAICT.Whether in a template or elsewhere, ::*
is a C++ token, only
usable in a type expression, in the context
class_name::*. It declares a pointer to member.
In your case, the typedef says that _data
is an alias for
a const pointer to a member of Vector2<T>
which has type
T const[2]
.
EDIT:
I got the actual definition wrong: _data
is an alias for an array[2] of const pointers to members of Vector2<T>
of type T
. This is obvious in the instantiation, where the object is initialized with two pointers to members.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With