oyyko
oyyko

Reputation: 49

Is temporary object initialization expresssion implicitly converted to xvalue while we access the this pointer?

The code is:

class A {
public:
  void f() { cout << this; }
};

int main() {
  A{};     // prvalue
  A{}.f(); // Is A{} in here converted to xvalue?
}

On https://en.cppreference.com/w/cpp/language/implicit_conversion, I learned the Temporary materialization rules. It tells an example:

struct S { int m; };
int i = S().m; // member access expects glvalue as of C++17;
               // S() prvalue is converted to xvalue

This example applies this rule: when performing a member access on a class prvalue;

However, I'm not sure if accessing the this pointer is a special case of accessing a member.

Are there any other conversion rules that can help us determine if a prvalue to xvalue conversion is happening here

Upvotes: -1

Views: 44

Answers (1)

user12002570
user12002570

Reputation: 1

A{}.f(); // Is A{} in here converted to xvalue?

Yes, as both A{} and S() are class prvalues and you're accessing the member f by writing A{}.f(), according to the below quoted statement, temporary materialization occurs and A{} prvalue is converted to xvalue.

In particular, from temporary materialization:

Temporary materialization occurs in the following situations:

  • when performing a member access on a class prvalue;

(emphasis mine)

Upvotes: 1

Related Questions