Taken from cppreference, why does the call to std::apply(add_generic, ...) fail to compile? Is there a way to fix it?
#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';
}
It fails with error:
[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::applyrequiresc++17, does your compiler havec++17support?add_genericisn't a function, it's a template. You can't deduce a type from something that doesn't have a type.add_genericfails, I think it's safe to say the OP is using a C++17 compiler+library (or something sufficiently close for the purposes of this question).