Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does it matter that `this` is an rvalue?

I know that the type of this is a prvalue ("pure" rvalue) pointer, and that it may be made a pointer-to-const and/or pointer-to-volatile (affecting accesses to its instance variables), by appending the keywords const or volatile to the end of the function definition to which it belongs.

I also know that this is sometimes (incorrectly) described as being a const pointer, perhaps as a way of saying "you can't make an assignment to this". As an rvalue, it is inherently unassignable, and so there is no need for the concept of a const rvalue.

I also know that in C++11, there are cases where being an rvalue or an lvalue can affect call resolution, but I've tried to work through the possibilities, and I'm not sure whether there's a case where it would actually matter to call resolution that this is an rvalue pointer rather than a const lvalue pointer.

Is there a case where this distinction makes a real difference, from a programmer's perspective, such as a context where an rvalue pointer can be used that a const lvalue pointer cannot be used, where a const lvalue pointer can be used that an rvalue pointer cannot, or where the difference affects call resolution?

like image 514
Theodore Murdock Avatar asked Feb 09 '16 22:02

Theodore Murdock


People also ask

What is the purpose of an rvalue reference?

Rvalue references is a small technical extension to the C++ language. Rvalue references allow programmers to avoid logically unnecessary copying and to provide perfect forwarding functions. They are primarily meant to aid in the design of higer performance and more robust libraries.

What is an rvalue in C++?

An rvalue is an expression that is not an lvalue. Examples of rvalues include literals, the results of most operators, and function calls that return nonreferences. An rvalue does not necessarily have any storage associated with it.

What is the difference between L value and R value references?

“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable. They are declared using the '&' before the name of the variable.

What does rvalue and lvalue stand for?

lvalues are “left values”: expressions that can be assigned to, which can be on the left of an assignment. rvalues are “right values”: everything else, which must be on the right of an assignment.


1 Answers

An obvious consequence of this being an prvalue is that &this is illegal. So, built-in unary operator & makes an example of "where a const lvalue pointer can be used that an rvalue pointer cannot". For the very same fundamental reason direct binding of references to this is not possible.

Also, prvalue-ness of this means that this itself cannot be const or volatile, since scalar rvalues cannot have cv-qualified types.

It is probably hard to build non-esoteric real-life code examples based on these distinctions.

like image 104
AnT Avatar answered Oct 06 '22 16:10

AnT