调用表达式中 Callable 参数的完美转发目的?
Purpose of perfect forwarding for Callable argument in invocation expression?
在 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()
是引用限定时:
struct Functor
{
void operator ()() const & { std::cout << "lvalue functor\n"; }
void operator ()() const && { std::cout << "rvalue functor\n"; }
};
在 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()
是引用限定时:
struct Functor
{
void operator ()() const & { std::cout << "lvalue functor\n"; }
void operator ()() const && { std::cout << "rvalue functor\n"; }
};