c++methodsthisnon-staticref-qualifier

implicit object parameter and this pointer


With reference to Non-static member functions, under

const-, volatile-, and ref-qualified member functions

it is mentioned:

A non-static member function can be declared with no ref-qualifier, with an lvalue ref-qualifier (the token & after the parameter list) or the rvalue ref-qualifier (the token && after the parameter list). During overload resolution, non-static cv-qualified member function of class X is treated as follows:

no ref-qualifier: the implicit object parameter has type lvalue reference to cv-qualified X and is additionally allowed to bind rvalue implied object argument

lvalue ref-qualifier: the implicit object parameter has type lvalue reference to cv-qualified X

rvalue ref-qualifier: the implicit object parameter has type rvalue reference to cv-qualified X

Note: unlike cv-qualification, ref-qualification does not change the properties of the this pointer: within a rvalue ref-qualified function, *this remains an lvalue expression.

In this context, what is the difference between the implicit object parameter and *this?


Solution

  • ref-qualifiers allow function overloading based on the type of reference of an expression.

    Since there are no pointers (or references) to references in C++ this can’t point to a (n rvalue) reference, so *this can’t be an rvalue.

    rvalue, lvalue and reference (intentionally) loose their relevance once the function is invoked.