Ross Bencina
Ross Bencina

Reputation: 4193

How to define a static member of a nested template (template class inside template class)

I have a template class S<T> with a nested template class S<T>::Q<M>. The inner class contains a static instance of itself.

How do I encode the definition of S<T>::Q<M>::q_ in the following code example? (The line marked with <---- error)

#include <iostream>

struct A {};
struct B {};

template<typename T>
struct S {

  template<typename M>
  struct Q {
    int x;
    Q() : x(1) {}

    static Q q_; 
  };
};

template<typename T, typename M>
typename S<T>::template Q<M> S<T>::Q<M>::q_; // <---- error

int main()
{
    std::cout << S<A>::Q<B>::q_.x;
}

Upvotes: 1

Views: 240

Answers (1)

Edgar Rokjān
Edgar Rokjān

Reputation: 17483

You should define it in the following way:

template<typename T>
template<typename M>
S<T>::Q<M> S<T>::Q<M>::q_ = Q();

Upvotes: 4

Related Questions