25

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.

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?

Motti
  • 110,860
  • 49
  • 189
  • 262
Clinton
  • 22,361
  • 15
  • 67
  • 163
  • Template specializations have to be at namespace scope, so the compiler is expecting you to do something like `template struct A1::A2<-1> { };` after the struct A1 definition, but that doesn't work either. I'm very interested to see an answer to this. On a sidenote, Visual C++ does allow your code, but it is not valid standard C++. – Sven Jun 10 '11 at 04:32

1 Answers1

11

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.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • 2
    What is the reasoning behind the standard not allowing such code? Since otherwise there is a compiler error, why wouldn't the standard instead direct the compiler to effectively insert the "DUMMY" templates itself? – Clinton Jun 10 '11 at 05:17
  • @Clinton, check [this](http://stackoverflow.com/questions/2537716/why-is-partial-specialziation-of-a-nested-class-template-allowed-while-complete) question. – Kirill V. Lyadvinsky Jun 10 '11 at 06:11