我将尝试编写一个函数(称之为apply_args()
),它接受特定的函数或函数对象以及调用该对象的参数,并使用完美转发来调用它。
例如:
auto fun = [](std::string a, std::string const& b) { return a += b; };
std::string s("world!");
// s is passing by lvalue ref,
// temporary object by rvalue ref
s = apply_args(fun, std::string("Hello, "), s);
我该如何实现这个功能?
std::invoke
。 - Daniel Hstd::invoke
是一个很好的选择,但它是 C++17 的;而这个问题标签是 C++11。 - max66