Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ::* (asterisk after scope op) means in C++ template?

Tags:

c++

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:

  1. What does the typedef line mean? I don't understand Vector2<T>::*
  2. Why _v has to be a static member? It seems it's not shared across instances, which doesn't follow static semantic in C++ AFAICT.
like image 427
Cuper Hector Avatar asked Aug 06 '14 11:08

Cuper Hector


1 Answers

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.

like image 115
James Kanze Avatar answered Nov 10 '22 21:11

James Kanze