mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 18:14:36 +03:00
Added ref-qualified member functions.
This commit is contained in:
29
CPP11.md
29
CPP11.md
@@ -31,6 +31,7 @@ C++11 includes the following new language features:
|
|||||||
- [inline-namespaces](#inline-namespaces)
|
- [inline-namespaces](#inline-namespaces)
|
||||||
- [non-static data member initializers](#non-static-data-member-initializers)
|
- [non-static data member initializers](#non-static-data-member-initializers)
|
||||||
- [right angle brackets](#right-angle-brackets)
|
- [right angle brackets](#right-angle-brackets)
|
||||||
|
- [ref-qualified member functions](#ref-qualified-member-functions)
|
||||||
|
|
||||||
C++11 includes the following new library features:
|
C++11 includes the following new library features:
|
||||||
- [std::move](#stdmove)
|
- [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++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++
|
```c++
|
||||||
@@ -583,6 +584,32 @@ typedef std::map<int, std::map <int, std::map <int, int> > > cpp98LongTypedef;
|
|||||||
typedef std::map<int, std::map <int, std::map <int, int>>> cpp11LongTypedef;
|
typedef std::map<int, std::map <int, std::map <int, int>>> 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
|
## C++11 Library Features
|
||||||
|
|
||||||
### std::move
|
### std::move
|
||||||
|
|||||||
29
README.md
29
README.md
@@ -74,6 +74,7 @@ C++11 includes the following new language features:
|
|||||||
- [inline-namespaces](#inline-namespaces)
|
- [inline-namespaces](#inline-namespaces)
|
||||||
- [non-static data member initializers](#non-static-data-member-initializers)
|
- [non-static data member initializers](#non-static-data-member-initializers)
|
||||||
- [right angle brackets](#right-angle-brackets)
|
- [right angle brackets](#right-angle-brackets)
|
||||||
|
- [ref-qualified member functions](#ref-qualified-member-functions)
|
||||||
|
|
||||||
C++11 includes the following new library features:
|
C++11 includes the following new library features:
|
||||||
- [std::move](#stdmove)
|
- [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++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++
|
```c++
|
||||||
@@ -1175,6 +1176,32 @@ typedef std::map<int, std::map <int, std::map <int, int> > > cpp98LongTypedef;
|
|||||||
typedef std::map<int, std::map <int, std::map <int, int>>> cpp11LongTypedef;
|
typedef std::map<int, std::map <int, std::map <int, int>>> 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
|
## C++11 Library Features
|
||||||
|
|
||||||
### std::move
|
### std::move
|
||||||
|
|||||||
Reference in New Issue
Block a user