DEV Community

Calin Baenen
Calin Baenen

Posted on

Why is `this` a pointer and not an rvalue reference in C++?

What was the thought behind making this a pointer of T, instead of T&&?

Top comments (6)

Collapse
 
pauljlucas profile image
Paul J. Lucas

First, your question should really be: "Why is this a pointer instead of a reference?" (Note: that's reference and not rvalue reference.)

The answer is simply because Bjarne invented this before he invented references.

Collapse
 
baenencalin profile image
Calin Baenen

I know this is where the question answer ends, and speculation begins. But:

Do you think they would have made this a reference (possibly for safety reasons, possibly just for style) had they came up with references before this?

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Probably; but at this point, I don't see why either my speculation or the question in general matters.

Thread Thread
 
baenencalin profile image
Calin Baenen

Yeah.. in hindsight, it was a very stupid question.

Collapse
 
oficsu profile image
Ofee Oficsu

Member functions appeared in C++ long before rvalue references (C++11) and even just references. But in C++23 with Deducing this proposal, you can do the following:

struct Foo {
  void bar(this Foo&& self) {
    // self is rvalue reference to Foo
  }
};
// ...
Foo foo;
std::move(foo).bar();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
pauljlucas profile image
Paul J. Lucas

This answer has nothing to do with the question.