C++11 – What is an rvalue Reference to Function Type?

c++11rvalue-reference

I have recently wrapped my mind around the C++0x's concepts of glvalues, xvalues and prvalues, as well as the rvalue references. However, there's one thing which still eludes me:

What is "an rvalue reference to function type"? It is literally mentioned many times in the drafts. Why was such a concept introduced? What are the uses for it?

Best Answer

I hate to be circular, but an rvalue reference to function type is an rvalue reference to function type. There is such a thing as a function type, e.g. void (). And you can form an rvalue reference to it.

In terms of the classification system introduced by N3055, it is an xvalue.

Its uses are rare and obscure, but it is not useless. Consider for example:

void f() {}
...
auto x = std::ref(f);

x has type:

std::reference_wrapper<void ()>

And if you look at the synopsis for reference_wrapper it includes:

reference_wrapper(T&) noexcept;
reference_wrapper(T&&) = delete; // do not bind to temporary objects

In this example T is the function type void (). And so the second declaration forms an rvalue reference to function type for the purpose of ensuring that reference_wrapper can't be constructed with an rvalue argument. Not even if T is const.

If it were not legal to form an rvalue reference to function, then this protection would result in a compile time error even if we did not pass an rvalue T to the constructor.