C++11 英特尔C&x2B+;编译器未能选择模板函数重载 < C++ >下面的代码用G++3.3.0正确编译,并导致我认为正确的行为(即,第一个函数被选中)。然而,英特尔的C++编译器(ICC170.4)无法编译;编译器指示存在多个可能的函数重载 #include <iostream> template<typename R, typename ... Args> static void f(R(func)(const int&, Args...)) { std::cout << "In first version of f" << std::endl; } template<typename R, typename ... Args, typename X = typename std::is_void<R>::type> static void f(R(func)(Args...), X x = X()) { std::cout << "In second version of f" << std::endl; } double h(const int& x, double y) { return 0; } int main(int argc, char** argv) { f(h); return 0; }

C++11 英特尔C&x2B+;编译器未能选择模板函数重载 < C++ >下面的代码用G++3.3.0正确编译,并导致我认为正确的行为(即,第一个函数被选中)。然而,英特尔的C++编译器(ICC170.4)无法编译;编译器指示存在多个可能的函数重载 #include <iostream> template<typename R, typename ... Args> static void f(R(func)(const int&, Args...)) { std::cout << "In first version of f" << std::endl; } template<typename R, typename ... Args, typename X = typename std::is_void<R>::type> static void f(R(func)(Args...), X x = X()) { std::cout << "In second version of f" << std::endl; } double h(const int& x, double y) { return 0; } int main(int argc, char** argv) { f(h); return 0; },c++11,templates,g++,overloading,icc,C++11,Templates,G++,Overloading,Icc,所以我的两个问题是:哪种编译器对于标准是正确的?您将如何修改此代码以使其能够编译?(请注意,f是一个面向用户的API,我希望避免修改它的原型) 请注意,如果我删除第二版f中的typename X=typename std::is\u void::type和X=X()参数,icc将对其进行良好编译。这是英特尔编译器17.0更新4中的一个错误。在这种情况下,GCC的行为是正确的。此问题在英特尔编译器18.0更新4及更高版本中得到解决。typename std::is\u void::type始终是b

所以我的两个问题是:哪种编译器对于标准是正确的?您将如何修改此代码以使其能够编译?(请注意,
f
是一个面向用户的API,我希望避免修改它的原型)


请注意,如果我删除第二版
f
中的
typename X=typename std::is\u void::type
X=X()
参数,icc将对其进行良好编译。

这是英特尔编译器17.0更新4中的一个错误。在这种情况下,GCC的行为是正确的。此问题在英特尔编译器18.0更新4及更高版本中得到解决。

typename std::is\u void::type
始终是
bool
。如果,您确定不想在
enable\u中使用它吗?不,
std::is\u void::type
的类型为
std::integral\u常量
std::is\u void::value\u type
bool
std::is\u void::value
true
false
。抱歉,你是对的。但是我关于使用
的问题是
中的
enable\u(如果t
有效)。在完整的代码中,我将
x
传递给另一个函数,该函数有两个重载,一个取
std::integral\u常量
参数,另一个取
std::integral\u常量
参数。我知道这很混乱,因为我删除了这两个函数的内容,似乎我声明了typename X,我只是想将测试用例剥离到最低限度,重现错误。
test.cpp(18): error: more than one instance of overloaded function "f" matches the argument list:
            function template "void f(R (*)(const int &, Args...))"
            function template "void f(R (*)(Args...), X)"
            argument types are: (double (const int &, double))
    f(h);
    ^