Reputation: 11047
For example, I want to get a list of maximum values from two sequences, left
and right
, and save the results in max_seq
, which are all previously defined and allocated,
std::transform(left.begin(), left.end(), right.begin(), max_seq.begin(), &max<int>);
But this won't compile because the compiler says
note: template argument deduction/substitution failed
I know I can wrapper "std::max" inside a struct
or inside a lambda
. But is there a way directly
use std::max
without wrappers?
Upvotes: 5
Views: 701
Reputation: 1
Template expansion and instanciation occur at compile time. So you can pass a template function only to templates.
You could pass an instanciated (templated) function at run-time (then it is an "ordinary" C++ function).
Upvotes: 0
Reputation: 109089
std::max
has multiple overloads, so the compiler is unable to determine which one you want to call. Use static_cast
to disambiguate and your code will compile.
static_cast<int const&(*)(int const&, int const&)>(std::max)
You should just use a lambda instead
[](int a, int b){ return std::max(a, b); }
Upvotes: 7