加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在调用表达式中完美转发Callable参数的目的?

发布时间:2020-12-16 06:53:57 所属栏目:百科 来源:网络整理
导读:在Scott Meyer的书 Effective Modern C++ on page 167(印刷版)中,他举了以下例子: auto timeFuncInvocation = [](auto func,auto... params) { // start timer; std::forwarddecltype(func)(func)( std::forwarddecltype(params)(params)... ); // stop tim
在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 functorn"; }
    void operator ()() const && { std::cout << "rvalue functorn"; }
};

Demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读