在 Scott Meyer 的书中 Effective Modern C++ on page 167 (打印版),他给出了以下示例:
auto timeFuncInvocation = [](auto&& func, auto&&... params) {
// start timer;
std::forward<decltype(func)>(func)(
std::forward<decltype(params)>(params)...
);
// stop timer and record elapsed time;
};
我完全理解 params
的完美转发,但我不清楚 func
的完美转发何时会相关。换句话说,上面的优点是什么:
auto timeFuncInvocation = [](auto&& func, auto&&... params) {
// start timer;
func(
std::forward<decltype(params)>(params)...
);
// stop timer and record elapsed time;
};
最佳答案
出于与参数相同的目的:所以当 Func::operator()
是 ref-qualified 时:
struct Functor
{
void operator ()() const & { std::cout << "lvalue functor\n"; }
void operator ()() const && { std::cout << "rvalue functor\n"; }
};
关于c++ - 调用表达式中 Callable 参数的完美转发目的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36920485/