开发者

C++: Nested template classes error "explicit specialization in non-namespace scope"

开发者 https://www.devze.com 2023-03-12 01:50 出处:网络
The following code: template <class T1> struct A1 { template <int INDEX> struct A2 { /* ... */ };

The following code:

template <class T1>
struct A1
{
  template <int INDEX>
  struct A2 { /* ... */ };

  template <>
  struct A2<-1> { /* ... */ };
};

int main() 
{
  A1<int>::A2<-1> x;
}

Gives this error:

prog.cpp:7:13: error: explicit specialization in non-namespace scope 'struct A1<T1>' prog.cpp:8:10: error: template parameters not used in partial specialization:

prog.cpp:8:10: error: 'T1'

How do I work around this error best? I tried this:

template <class T1>
struct A1
{
  template <int INDEX, class DUMMY = void>
  struct A2 { /* ... */ };

  template <class DUMMY>
  struct A2<-1, DUMMY> { /* ... */ };
};

int main() 
{
  A1<int>::A2<-1> x;
}

Which seems to work but also seems like a bit of a fudge.

开发者_Python百科

Is there a better way of working around this?

I have looked through previous answers and could only find ones with functions in classes, not classes in classes. I also found the "DUMMY" trick in other answers but was wondering if there is a nicer solution.

Also, as a side note, is the first code allowed by C++0x?


It is not allowed to explicitly specialize A2 without specializing A1 (§14.7.3/18). C++0x has the same restriction (n3242 §14.7.3/16). At the same moment a partial specialization of a nested class is allowed. So the trick with a dummy class is Ok.

0

精彩评论

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

关注公众号