开发者

Initialize static member of template inner class

I have problem with the syntax needed to initialize a static member in a class template. Here is the code (I tried to reduce it as much as I could):

template <typename T>
struct A
{
  template &l开发者_JAVA技巧t;typename T1>
  struct B
  {
    static T1 b;
  };

  B<T> b;
  typedef B<T> BT;
  T val() { return b.b; }
};

template <typename T>
T A<T>::BT::b;

struct D
{
  D() : d(0) {}
  int d;
};

int main()
{
  A<D> a;
  return a.val().d;
}

With g++, the error I get is:

error: too few template-parameter-lists

Any ideas how to initialize b?

Note that I would like to keep the typedef, as in my real code, B is way more complex than that.


Change the definition of b to the following:

template <typename T> template<typename T1>
T1 A<T>::B<T1>::b;

Notice that the typedef and B<T1> don't necessarily specify the same type: While the typedef relies on T being passed to B, B<T1> relies on the template parameter T1 being passed. So you cannot use the typedef here to specify a definition for b in B<T1>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜