Search code examples
c++templatessyntaxsemantics

Meaning of template syntax with scope qualifier


I saw this recently:

template <class U> struct ST
{
...
};
template <class U, class V>
struct ST<U V::*>
{
...
};

I assume that the second template is a specialization of the first.

But what is the semantics of U V::* ???


Solution

  • That means "pointer-to-member of class V where the type of the member is U ". For instance,

    struct X
    {
        int x = 0;
    };
    
    // ...
    
    int X::*p = &X::x;     // <== Declares p as pointer-to-member
    
    ST<decltype(&X::x)> s; // <== Will instantiate your template specialization,
                           //     with U = int and V = X
    
    ST<int X::*> t;        // <== Will instantiate your template specialization,
                           //     with U = int and V = X