self
is proposed keyword used to refer the current object in C++ (the same as *this
). self
makes more sense for the language because of the following reasons
- You can not change the pointer of
self
because it is a reference.this
has a special rule that makes it a prvalue, butself
does not need any exceptions. this
is a special pointer that must point to a valid object however other pointers are not required to do so. References must point to a valid object soself
is more suited for this purpose thanthis
.return self
instead ofreturn *this
from assignment operators looks cleaner.- Using raw pointers in C++ is usually discouraged (references are preferred).
this
could encourage programmers to use pointers instead of references. - It is easier to teach new programmers how to use references instead of pointers.
- Ref-qualifiers already qualify the type of
this
reference not pointer so usingself
reference instead ofthis
pointer makes more sense.
this
points to current object while self
references it. self
should not break the C code that is being compiled with a C++ compiler (e.g. Visual Studio). Lots of C projects use self
as name for a variable that points to the current object so self
should not be a reserved keyword (just like override
or final
).
Bjarne Stroustrup created "C with classes" in 1979. It was a superset of C and had a this
pointer in it. "C with classes" did not have references. When "C with classes" evolved to C++ in 1983 references were added to the language but it was decided to leave this
as a pointer for backwards compatibility. Stroustrup himself has said that the only reason why this
is not a reference is because it was introduced when C++ had no references (http://www.stroustrup.com/bs_faq2.html#this).
Add #include "self"
or #define self (*this)
to the top of your source files or pass -include self
to gcc/clang or /FI self
to Visual C++ compiler.
class Foo
{
Foo& operator=(const Foo& other)
{
// do the assignment
return self;
}
};
TODO