我想通过它的模板参数的类型特征来专门化模板类上的成员函数,但是我的前向声明显然是不正确的。有一个简单的解决方法吗?按类型专门化模板类成员函数
#include <type_traits>
template <typename T>
class TTest{
public:
T data;
// edited to comment this out, template<typename U>
bool operator !=(const TTest& other) const;
};
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value, bool>::type
TTest<T>::operator !=(const TTest<T>& other) const{
return true;
}
template<typename T>
bool TTest<T>::operator !=(const TTest<T>& other) const{
return false;
}
int main(){
TTest<size_t> t1;
TTest<int> t2;
}
锵告诉我:
templateTest.cpp:13:11: error: out-of-line definition of 'TTest::operator!='
differs from the declaration in the return type
TTest<T>::operator !=(const TTest<T>& other) const{
^
templateTest.cpp:8:8: note: previous declaration is here
bool operator !=(const TTest& other) const;
^
1 error generated.
此代码没有意义。成员模板对模板参数具有* no *依赖性,所以没有什么特别的。 –
你是否意识到'const TTest&other'是指你rmember模板中的'const TTest&other'? –
你不能部分地专门化一个函数模板。如果你想要一个无符号T和非无符号T的版本,那么你需要重载+ SFINAE或部分专门化整个类模板。 – dyp