相关疑难解决方法(0)

将std :: enable_if与匿名类型参数一起使用

我尝试使用std::enable_if未使用和未命名的类型参数,以免扭曲return类型.但是,以下代码无法编译.

#include <iostream>

template <typename T, typename = std::enable_if_t<!std::is_integral<T>::value>>
T foo() { std::cout << "non-integral" << std::endl; return T(); }

template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
T foo() { std::cout << "integral" << std::endl; return T(); }

int main() {
  foo<float>();
  foo<int>();
}
Run Code Online (Sandbox Code Playgroud)

编译器说:

7:3: error: redefinition of 'template<class T, class> T foo()'
4:3: note: 'template<class T, class> T foo()' previously declared here
 In function 'int main()':
11:12: error: no matching function for call to 'foo()' …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae enable-if c++11

6
推荐指数
2
解决办法
640
查看次数

标签 统计

c++ ×1

c++11 ×1

enable-if ×1

sfinae ×1

templates ×1