相关疑难解决方法(0)

std::enable_if 的第二个参数有什么用?

我对 std::enable_if 的第二个参数感到困惑。在使用 int 返回类型时,我们可以使用:

template <class T>
typename std::enable_if<mpi::is_builtin<T>::value, int>::type
foo() { return 1; }
Run Code Online (Sandbox Code Playgroud)

但是如何在参数或模板中使用enable_if?在这种情况下,下面的too函数有什么区别:

template<class T , 
       class = typename std::enable_if<std::is_integral<T>::value>::type >
T too(T t) { std::cout << "here" << std::endl; return t; }

int too(int t) { std::cout << "there" << std::endl; return t; }
Run Code Online (Sandbox Code Playgroud)

谢谢。

c++ generics templates enable-if c++11

5
推荐指数
1
解决办法
1429
查看次数

标签 统计

c++ ×1

c++11 ×1

enable-if ×1

generics ×1

templates ×1