14

In Scott Meyer's book Effective Modern C++ on page 167 (of the print version), he gives the following example:

auto timeFuncInvocation = [](auto&& func, auto&&... params) {
  // start timer;
  std::forward<decltype(func)>(func)(
    std::forward<decltype(params)>(params)...
  );
  // stop timer and record elapsed time;
};

I completely understand the perfect forwarding of params, but it is unclear to me when perfect forwarding of func would ever be relevant. In other words, what are the advantages of the above over the following:

auto timeFuncInvocation = [](auto&& func, auto&&... params) {
  // start timer;
  func(
    std::forward<decltype(params)>(params)...
  );
  // stop timer and record elapsed time;
};
Daisy Sophia Hollman
  • 6,046
  • 6
  • 24
  • 35

1 Answers1

13

For the same purpose as for arguments: so when Func::operator() is a ref-qualified:

struct Functor
{
    void operator ()() const &  { std::cout << "lvalue functor\n"; }
    void operator ()() const && { std::cout << "rvalue functor\n"; }
};

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 4
    Remove the `const` part, then it makes *better* sense. – Nawaz Apr 28 '16 at 16:44
  • no, as `const` rvalue reference will prefer `const &` overload to `&&` overload: `const Functor f() { return Functor{}; }; f()();` [example](http://coliru.stacked-crooked.com/a/3e1b8f476f9e3f05) – Dev Null Aug 10 '17 at 11:53