【发布时间】:2012-10-02 12:52:17
【问题描述】:
以下代码无法使用 Intel C++ 2013 编译。
#include <type_traits>
#include <iostream>
template <
typename T,
typename std::enable_if<std::is_integral<T>::value>::type
>
void myfunc( T a)
{
std::cout << a << std::endl;
}
template <
typename T,
typename std::enable_if<!std::is_integral<T>::value>::type
>
void myfunc( T a)
{
std::cout << a << std::endl;
}
int main()
{
double a;
int b;
myfunc(a);
myfunc(b);
return 0;
}
这是错误输出:
ConsoleApplication1.cpp(33): error : no instance of overloaded function "myfunc" matches the argument list
1> argument types are: (double)
1> myfunc(a);
1> ^
1>
1>ConsoleApplication1.cpp(34): error : no instance of overloaded function "myfunc" matches the argument list
1> argument types are: (int)
1> myfunc(b);
1> ^
1>
我哪里错了?
【问题讨论】:
-
std::cout << T?你的意思是a? -
您不能有 void 类型的模板非类型参数。告诉 enable_if 给你一个 int 类型并提供一个默认值
标签: c++ templates c++11 sfinae enable-if