Rvalue function reference

typedef void(&&RF)(void* p); RF rf() { return f; } int ay[10] = { 0 }; typedef int(&&RA)[10]; RA ra() { return ay; // error } cout << is_lvalue_reference<decltype(rf())>::value << endl; // 1 

The C ++ link says: "rvalue references to functions are processed as lvalues ​​name or not."

But I can’t understand what these considerations are. I assume that perhaps the function name is always an lvalue. Therefore, it must retain its lvalue attribute and ensure that the name of the function is passed wherever it can be called, for example rf()(NULL) . Then the name of the array became invisible to me. I think this is always an lvalue value, so I wrote the code above to check this and got an error.

Who can point to the true cause of all this?

+8
c ++ c ++ 11 rvalue-reference
source share
1 answer

N3055 briefly addresses the issue of rvalue function references:

In addition, rvalue links (for example, traditional lvalue links) can be associated with functions. Considering the rvalue reference return value as an rvalue, however, introduces a new concept of the rvalue function into the language. There was no such idea before - the lvalue function used in the rvalue context becomes the r-value for the function pointer, and not the rvalue function - therefore, the current draft of the standard does not describe how such values ​​should be processed. In particular, function calls and conversions for working with pointers are specified in terms of lvalues ​​functions, so the most likely use of rvalue references for undefined functions is in the current edition.

Functions do not have a lifespan or storage, so the distinction lvalue / rvalue does not make sense to them. On the other hand, if you allow the rvalues ​​functions to exist, you need to solve the problems discussed in this paragraph. In light of this, the forced value of all function values ​​for lvalues ​​seems to me a reasonable solution.

Another solution, I believe, would be to completely prohibit rvalues, so any attempt to create an rvalue reference for a function type will result in a poorly formed program. I do not know if this approach has been considered, but I assume that this will cause inconvenience with general programming.

On the other hand, for any type of object, including array types, there is a significant difference between lvalues ​​and rvalues. Therefore, the language forbids you to bind an rvalue reference to an object type to an lvalue of an object type. I'm not sure why you are surprised that your code does not compile.

+9
source share

All Articles