我试图更好地理解 C++11 中的 std::enable_if
并且一直在尝试编写一个最小的示例:一个类 A
带有成员函数 void foo()
根据类模板中的类型 T
具有不同的实现。
下面的代码给出了期望的结果,但我还没有完全理解它。为什么版本 V2
有效,但 V1
无效?为什么需要“冗余”类型 U
?
#include <iostream>
#include <type_traits>
template <typename T>
class A {
public:
A(T x) : a_(x) {}
// Enable this function if T == int
/* V1 */ // template < typename std::enable_if<std::is_same<T,int>::value,int>::type = 0>
/* V2 */ template <typename U=T, typename std::enable_if<std::is_same<U,int>::value,int>::type = 0>
void foo() { std::cout << "\nINT: " << a_ << "\n"; }
// Enable this function if T == double
template <typename U=T, typename std::enable_if<std::is_same<U,double>::value,int>::type = 0>
void foo() { std::cout << "\nDOUBLE: " << a_ << "\n"; }
private:
T a_;
};
int main() {
A<int> aInt(1); aInt.foo();
A<double> aDouble(3.14); aDouble.foo();
return 0;
}
是否有更好的方法来实现预期的结果,即根据类模板参数对 void foo()
函数进行不同的实现?
最佳答案
我知道这不会完全回答你的问题,但它可能会给你更多的想法和理解如何使用 std::enable_if
。
您可以将您的 foo 成员函数替换为以下并具有相同的功能:
template<typename U=T> typename std::enable_if<std::is_same<U,int>::value>::type
foo(){ /* enabled when T is type int */ }
template<typename U=T> typename std::enable_if<std::is_same<U,double>::value>::type
foo(){ /* enabled when T is type double */ }
不久前,我对 enable_if 的工作原理有了很好的理解,但遗憾的是,我忘记了它的大部分复杂之处,只记得更实用的使用方法。
关于c++ - enable_if : minimal example for void member function with no arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41667905/