下面的代码没问题:
template <class T>
std::enable_if<std::is_atomic<T>::value, bool>
foo(T t) { return true; }
template <class T>
std::enable_if<tmp::is_sequence<T>::value, bool>
foo(T t) { return false; }
int main(void){
foo(1); // return true
auto std::vector<int> a{2};
foo(a); // return false
}
但是当我使用一个类来捆绑它们时,它无法编译:
template <class T>
class test {
public:
std::enable_if<std::is_atomic<T>::value, bool>
foo(T t) { return true; }
std::enable_if<tmp::is_sequence<T>::value, bool>
foo(T t) { return false; }
};
int main(...) {
test<int> obj;
obj.foo(1);
test<std::vector<int>> obj2;
std::vector<int> tmp{2};
obj2.foo(tmp);
}
铿锵++打印:
error: functions that differ only in their return type cannot be overloaded
所以我写了一些东西来欺骗编译器(在第二个中添加一个 S foo
):
template <class S>
std::enable_if<tmp::is_sequence<T>::value, bool>
foo(T t) { return false; }
它仍然无法工作:
error: no type named 'type' in 'std::enable_if<false, bool>'
我怎样才能让它在课堂上工作?