Trying to understand why the following example fails to compile:
#include <functional>
template <typename F>
void f1(F&& f)
{
std::forward<F>(f)("hi");
}
template <typename F>
void f2(F&& f)
{
std::invoke(f, "hi"); // works but can't perfect forward functor
}
template <typename F>
void f3(F&& f)
{
std::invoke<F>(f, "hi");
}
int main()
{
f1([](const char*) {}); // ok
f2([](const char*) {}); // ok
f3([](const char*) {}); // error
}
cppreference says the following about std::invoke:
- Invoke the Callable object
fwith the parameters args. As byINVOKE(std::forward<F>(f), std::forward<Args>(args)...). This overload participates in overload resolution only ifstd::is_invocable_v<F, Args...>is true.
So why is f3 not equivalent to f1?