Как правильно выполнить частичную специализацию метода для шаблона класса?
template <typename T1, typename T2>
class MyClass {
double foo(T1 a, T2 b);
};
template <>
double MyClass<float, int>::foo(float a, int b) { // OK
return (double)a+b;
}
template <typename T1>
double MyClass<T1, short>::foo(T1 a, short b) {
return (double)(a*b);
}
// MSVS 2005:
// error C2244: 'MyClass<T1,T2>::foo' : unable to match function definition to an existing declaration
// see declaration of 'MyClass<T1,T2>::foo'
// Intel C++ 11.1
// error: template argument list must match the parameter list
// double MyClass<T1, short>::foo(T1 a, short b) {
// ^
// http://www.comeaucomputing.com/tryitout/
// "ComeauTest.c", line 21: error: template argument list must match the parameter list
// double MyClass<T1, short>::foo(T1 a, short b) {
// ^
Здравствуйте, Andrew_manik, Вы писали:
A_>Как правильно выполнить частичную специализацию метода для шаблона класса?
у функций\методов не бывает частичной специализации
частичная специализация бывает у
классов
template <typename T1, typename T2>
class MyClass {
double foo(T1 a, T2 b);
};
template <>
double MyClass<float, int>::foo(float a, int b) { // OK
return (double)a+b;
}
template <typename T1>
class MyClass<T1, short> {
double foo(T1 a, short b) {
return (double)(a*b);
}
};