GCC está en el claro, aquí. MSVC tiene una extensión no estándar que permite la especialización en clase. El estándar, sin embargo, dice:
14.7.3.2:
2. An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member. An explicit specialization of a member function, member class or static data member of a class template shall be declared in the namespace of which the class template is a member.
Además, no puede especializar parcialmente una función. (Aunque estoy seguro acerca de los detalles de su caso, que sería el golpe final.)
Usted podría hacer esto:
#include <iostream>
struct true_type {};
struct false_type {};
template <typename T, typename U>
struct is_same : false_type
{
static const bool value = false;
};
template <typename T>
struct is_same<T, T> : true_type
{
static const bool value = true;
};
struct tag1 {};
struct tag2 {};
template< typename T >
struct C
{
typedef T t_type;
template< typename Tag >
void foo(t_type pX)
{
foo_detail(pX, is_same<Tag, tag1>());
}
private:
void foo_detail(t_type, const true_type&)
{
std::cout << "In tag1 version." << std::endl;
}
void foo_detail(t_type, const false_type&)
{
std::cout << "In not tag1 version." << std::endl;
}
};
int main(void)
{
C<int> c;
c.foo<tag1>(int());
c.foo<tag2>(int());
c.foo<double>(int());
}
Aunque esto es algo feo.
Esto es similar a la solución que estoy usando, ¡gracias! Me pregunto si hay una manera de hacer esto de forma menos veraz usando Boost. – jwfearn
No realmente, no se vuelve más fácil en general. Necesita una función de ayuda para evitar el problema de * "la clase adjunta tiene que estar explícitamente especializada" * y necesita mover esa en una clase para hacer una especialización parcial. –