2

The following is valid c++

void g() {}
void (&&r)(void) = g;

See "Can an rvalue reference bind to a function?" thread for details.

My question is: What is the reason that made this possible?

Community
  • 1
  • 1
a.lasram
  • 4,371
  • 1
  • 16
  • 24

1 Answers1

2

I suspect that it is to provide consistency between plain functions and function objects (including lambdas).

If you make a copy of a pointer or reference to a plain function, the state (static locals) is shared, which is somewhat the same as moving (leaving the old copy in an indeterminate but valid state).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Lambdas don't provide a conversion to reference-to-function types, although they may provide a conversion to pointer-to-function types. –  Jun 10 '13 at 19:30
  • @hvd: Sure, but I'm thinking of the case where a templated algorithm accepts a function object. `template remove_if(Container&, Predicate&&)`-type scenarios – Ben Voigt Jun 10 '13 at 19:32
  • Ah, okay. For that, I would personally have expected to treat `T &&` as `T &` or `T` if `T` is a function type, just like it does when `T` is an lvalue reference type, but I suppose this is another valid way to solve that. –  Jun 10 '13 at 19:36
  • @hvd: I would take it as `T` as well, but I can also imagine that some algorithms might somehow care whether the predicate has moveable state. – Ben Voigt Jun 10 '13 at 19:37