From 1a5af17e8ac3d0399f3ea63f207e0517762e72a0 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Thu, 13 Jun 2019 22:28:43 -0400 Subject: [PATCH] Added ref-qualified member functions. --- CPP11.md | 29 ++++++++++++++++++++++++++++- README.md | 29 ++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/CPP11.md b/CPP11.md index 0ec8212..caed545 100644 --- a/CPP11.md +++ b/CPP11.md @@ -31,6 +31,7 @@ C++11 includes the following new language features: - [inline-namespaces](#inline-namespaces) - [non-static data member initializers](#non-static-data-member-initializers) - [right angle brackets](#right-angle-brackets) +- [ref-qualified member functions](#ref-qualified-member-functions) C++11 includes the following new library features: - [std::move](#stdmove) @@ -575,7 +576,7 @@ class Human { }; ``` -### Right angle Brackets +### Right angle brackets C++11 is now able to infer when a series of right angle brackets is used as an operator or as a closing statement of typedef, without having to add whitespace. ```c++ @@ -583,6 +584,32 @@ typedef std::map > > cpp98LongTypedef; typedef std::map>> cpp11LongTypedef; ``` +### Ref-qualified member functions +Member functions can now be qualified depending on whether `*this` is an lvalue or rvalue reference. + +```c++ +struct Bar { + // ... +}; + +struct Foo { + Bar getBar() & { return bar; } + Bar getBar() const& { return bar; } + Bar getBar() && { return std::move(bar); } +private: + Bar bar{}; +}; + +Foo foo{}; +Bar bar = foo.getBar(); // calls `Bar getBar() &` + +const Foo foo2{}; +Bar bar2 = foo2.getBar(); // calls `Bar Foo::getBar() const&` + +Foo{}.getBar(); // calls `Bar Foo::getBar() &&` +std::move(foo).getBar(); // calls `Bar Foo::getBar() &&` +``` + ## C++11 Library Features ### std::move diff --git a/README.md b/README.md index 985c994..750e71f 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ C++11 includes the following new language features: - [inline-namespaces](#inline-namespaces) - [non-static data member initializers](#non-static-data-member-initializers) - [right angle brackets](#right-angle-brackets) +- [ref-qualified member functions](#ref-qualified-member-functions) C++11 includes the following new library features: - [std::move](#stdmove) @@ -1167,7 +1168,7 @@ class Human { }; ``` -### Right angle Brackets +### Right angle brackets C++11 is now able to infer when a series of right angle brackets is used as an operator or as a closing statement of typedef, without having to add whitespace. ```c++ @@ -1175,6 +1176,32 @@ typedef std::map > > cpp98LongTypedef; typedef std::map>> cpp11LongTypedef; ``` +### Ref-qualified member functions +Member functions can now be qualified depending on whether `*this` is an lvalue or rvalue reference. + +```c++ +struct Bar { + // ... +}; + +struct Foo { + Bar getBar() & { return bar; } + Bar getBar() const& { return bar; } + Bar getBar() && { return std::move(bar); } +private: + Bar bar{}; +}; + +Foo foo{}; +Bar bar = foo.getBar(); // calls `Bar getBar() &` + +const Foo foo2{}; +Bar bar2 = foo2.getBar(); // calls `Bar Foo::getBar() const&` + +Foo{}.getBar(); // calls `Bar Foo::getBar() &&` +std::move(foo).getBar(); // calls `Bar Foo::getBar() &&` +``` + ## C++11 Library Features ### std::move