25
votes

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?

1
Template specializations have to be at namespace scope, so the compiler is expecting you to do something like template<typename T> struct A1<T>::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

1 Answers

11
votes

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.