gpt4 book ai didi

C++ 可变参数模板 : different priorities in gcc and nvcc/intel

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:25 25 4
gpt4 key购买 nike

下一段代码有问题:

#include <iostream>
#include <functional>
using namespace std;

template<typename... Args>
inline double foo(function<double (Args...)> fun, Args... args) {
return fun(args...);
}

template<typename... Args, typename... ArgsTmp>
double foo(function<double (Args...)> fun, ArgsTmp... args) {
return foo(fun, 1., args...);
}

int main() {

function<double (double, double, double)> fun = [&](double x, double y, double z) -> double {
return x+y+z;
};

cout << foo(fun) << endl;
}

当我用 gcc 编译它时一切正常。但是,如果我尝试编译它,例如,使用 intel 或 nvcc-7.5(这实际上是我的目标),我会收到以下错误:

more than one instance of overloaded function "foo" matches the argument list:
function template "double foo(std::vector<limits, std::allocator<limits>>, std::function<double (Args...)>, Args...)"
function template "double foo(std::vector<limits, std::allocator<limits>>, std::function<double (Args...)>, ArgsTmp...)"
argument types are: (std::vector<limits, std::allocator<limits>>, std::function<double (double, double, double)>, double, double, double)
...
1 error

有什么方法可以让编译器明确知道当 Args==ArgsTmp(在第二个模板中)时,应该拒绝以下 foo() 的实现?

最佳答案

您可以尝试 SFINAE 和 enable_if

template<typename... Args, typename... ArgsTmp>
std::enable_if_t<!std::is_same<std::tuple<Args...>,
std::tuple<ArgsTmp...>>::value,
double>
foo(function<double (Args...)> fun, ArgsTmp... args) {
return foo(fun, 1., args...);
}

关于C++ 可变参数模板 : different priorities in gcc and nvcc/intel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33550295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com