双模板函数重载失败
Double templated function overload fails
我有一个模板class,具有各种模板功能。其中之一需要超载(几次)。
基本上 - 如果我的 class 不是模板, 这些将是我的函数:
class OtherClass
{
public:
template<class T> T foo(T &t, const std::string &trash);
};
template<class T>
T OtherClass::foo(T &t, const std::string &trash)
{
return t; //...
}
template<>
std::wstring OtherClass::foo<std::wstring>(std::wstring &w, const std::string &trash)
{
return w; //...
}
这个:
int main(...)
{
int i = 0;
std::wstring w;
OtherClass o;
i = o.foo(i, std::string(""));
w = o.foo(w, std::string(""));
}
我的模板class看起来像:
template<class MStr>
class OtherClass
{
public:
template<class TVal> TVal foo(TVal &t, const MStr &trash);
};
//Which leads to the function definition
template<class MStr>
template<class TVal>
TVal OtherClass<MStr>::foo(TVal &t, const MStr &trash)
{
return t; //...
}
我想要的...(以整数为例)
template<class MStr>
template<>
int OtherClass<MStr>::foo<int>(int &t, const MStr &trash)
{
return t; //...
}
欢迎来到
的土地
C2768: illegal use of explicit template arguments
和
C2244: unable to match function definition
1>...\test\main.cpp(74): error C2768: 'OtherClass<MStr>::foo' : illegal use of explicit template arguments
1>...\test\main.cpp(74): error C2768: 'OtherClass<MStr>::foo' : illegal use of explicit template arguments
1>...\test\main.cpp(74): error C2244: 'OtherClass<MStr>::foo' : unable to match function definition to an existing declaration
1> definition
1> 'int OtherClass<MStr>::foo<int>(int &,const MStr &)'
1> existing declarations
1> 'TVal OtherClass<MStr>::foo(TVal &,const MStr &)'
1>
1>Build FAILED.
我一直在测试,并在 Google 和 Whosebug 中寻找时间...到目前为止最好的 answer/question,但似乎不适用于我 this.
问:有没有人可以指出正确的方向,或者有解决办法来解决这个问题?
回避这个问题的一种方法是将 int
版本声明为重载而不是模板特化:
template<class MStr>
class OtherClass
{
public:
template<class TVal> TVal foo(TVal &t, const MStr &trash);
int foo(int &t, const MStr &trash);
};
则定义为:
template<class MStr>
int OtherClass<MStr>::foo(int &t, const MStr &trash)
{
return t; //...
}
如果你有很多过载情况,它不是特别好,但它可能胜过任何其他解决方案。
我有一个模板class,具有各种模板功能。其中之一需要超载(几次)。
基本上 - 如果我的 class 不是模板, 这些将是我的函数:
class OtherClass
{
public:
template<class T> T foo(T &t, const std::string &trash);
};
template<class T>
T OtherClass::foo(T &t, const std::string &trash)
{
return t; //...
}
template<>
std::wstring OtherClass::foo<std::wstring>(std::wstring &w, const std::string &trash)
{
return w; //...
}
这个:
int main(...)
{
int i = 0;
std::wstring w;
OtherClass o;
i = o.foo(i, std::string(""));
w = o.foo(w, std::string(""));
}
我的模板class看起来像:
template<class MStr>
class OtherClass
{
public:
template<class TVal> TVal foo(TVal &t, const MStr &trash);
};
//Which leads to the function definition
template<class MStr>
template<class TVal>
TVal OtherClass<MStr>::foo(TVal &t, const MStr &trash)
{
return t; //...
}
我想要的...(以整数为例)
template<class MStr>
template<>
int OtherClass<MStr>::foo<int>(int &t, const MStr &trash)
{
return t; //...
}
欢迎来到
的土地
C2768: illegal use of explicit template arguments
和
C2244: unable to match function definition
1>...\test\main.cpp(74): error C2768: 'OtherClass<MStr>::foo' : illegal use of explicit template arguments
1>...\test\main.cpp(74): error C2768: 'OtherClass<MStr>::foo' : illegal use of explicit template arguments
1>...\test\main.cpp(74): error C2244: 'OtherClass<MStr>::foo' : unable to match function definition to an existing declaration
1> definition
1> 'int OtherClass<MStr>::foo<int>(int &,const MStr &)'
1> existing declarations
1> 'TVal OtherClass<MStr>::foo(TVal &,const MStr &)'
1>
1>Build FAILED.
我一直在测试,并在 Google 和 Whosebug 中寻找时间...到目前为止最好的 answer/question,但似乎不适用于我 this.
问:有没有人可以指出正确的方向,或者有解决办法来解决这个问题?
回避这个问题的一种方法是将 int
版本声明为重载而不是模板特化:
template<class MStr>
class OtherClass
{
public:
template<class TVal> TVal foo(TVal &t, const MStr &trash);
int foo(int &t, const MStr &trash);
};
则定义为:
template<class MStr>
int OtherClass<MStr>::foo(int &t, const MStr &trash)
{
return t; //...
}
如果你有很多过载情况,它不是特别好,但它可能胜过任何其他解决方案。