3
来自cppreference,为什么std::apply(add_generic, ...)
的调用不能编译?有没有办法解决它?为什么std :: apply使用泛型函数失败?
#include <iostream>
#include <tuple>
int add(int first, int second)
{
return first + second;
}
template<typename T>
T add_generic(T first, T second)
{
return first + second;
}
int main()
{
std::cout << std::apply(add, std::make_tuple(1,2)) << '\n';
// template argument deduction/substitution fails
std::cout << std::apply(add_generic, std::make_tuple(2.0f,3.0f)) << '\n';
}
它fails错误:
[x86-64 gcc 7 (snapshot)] error: no matching function for call to 'apply(, std::tuple)' [x86-64 gcc 7 (snapshot)] note: couldn't deduce template parameter '_Fn'
没有看到你所得到的错误,我想这是可能是因为无法正确推断参数类型“T”? –
你是如何编译代码的? 'std :: apply'需要'C++ 17',你的编译器是否支持'C++ 17'? – Rogus
'add_generic'不是一个函数,它是一个模板。你不能从没有类型的东西中推导出一个类型。 – chris