C++, специализация шаблона

erotic

Почему нельзя делать так:
 
template <int N>
class MC
{
template <int D>
struct Res
{
static const int tp = D;
};

template<>
struct Res<15>
{
static const int tp = 0;
};
public:
static const int val = Res<N>::tp;
};


Под MSVC2005 компилируется нормально, под gcc - ошибка:
test.cpp error: explicit specialization in non-namespace scope `class MC<N>
test.cpp error: enclosing class templates are not explicitly specialized
test.cpp error: template parameters not used in partial specialization:
test.cpp error: `N'

Хотя по стандарту специализация должна находиться в том же неймспейсе, что и сам шаблон. Вроде, там она и находится.

barbos

Похоже, недавно столкнулся с такой же проблемой. Хотелось бы узнать её решение.
В моём случае, кажется, помогло вынести реализацию за определение класса.

erotic

Ага, так и сделал

procenkotanya

Похоже, что в данном случае MSVC не соответствует стандарту — такая специализация на самом деле запрещена. Это подтверждает и проверка с помощью компилятора Comeau:
"ComeauTest.c", line 10: error: explicit specialization is not allowed in the
current scope
template<>
^
см. также http://msdn.microsoft.com/library/default.asp?url=/library/e...

erotic

shit
This still breaks, and rightly so: According to the standard, the declaration must appear in the namespace scope containing X, which in this case is global scope.
I don't know a workaround for this. For now you apparently can't specialize template members.
Оставить комментарий
Имя или ник:
Комментарий: