-1

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

colin
  • 49
  • 2
  • *"`A{}.f();` // Is A{} in here converted to xvalue?"* Yes. Why do you think it is different from `S().m`? Just because you're using `this` inside `f`? – Jason Aug 22 '22 at 08:27
  • As both `A{}` and `S()` are class prvalues and you're accessing the member `f` by writing `A{}.f()`, according to the above quoted statement, temporary materialization occurs and `A{}` prvalue is converted to xvalue. – Jason Aug 22 '22 at 08:33

1 Answers1

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)

Jason
  • 36,170
  • 5
  • 26
  • 60