mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 18:14:36 +03:00
Trailing return types.
This commit is contained in:
33
CPP11.md
33
CPP11.md
@@ -32,6 +32,7 @@ C++11 includes the following new language features:
|
|||||||
- [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)
|
- [ref-qualified member functions](#ref-qualified-member-functions)
|
||||||
|
- [trailing return types](#trailing-return-types)
|
||||||
|
|
||||||
C++11 includes the following new library features:
|
C++11 includes the following new library features:
|
||||||
- [std::move](#stdmove)
|
- [std::move](#stdmove)
|
||||||
@@ -612,6 +613,38 @@ std::move(foo).getBar(); // calls `Bar Foo::getBar() &&`
|
|||||||
std::move(foo2).getBar(); // calls `Bar Foo::getBar() const&&`
|
std::move(foo2).getBar(); // calls `Bar Foo::getBar() const&&`
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Trailing return types
|
||||||
|
C++11 allows functions and lambdas an alternative syntax for specifying their return types.
|
||||||
|
```c++
|
||||||
|
int f() {
|
||||||
|
return 123;
|
||||||
|
}
|
||||||
|
// vs.
|
||||||
|
auto f() -> int {
|
||||||
|
return 123;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```c++
|
||||||
|
auto g = []() -> int {
|
||||||
|
return 123;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
This feature is especially useful when certain return types cannot be resolved:
|
||||||
|
```c++
|
||||||
|
// NOTE: This does not compile!
|
||||||
|
template <typename T, typename U>
|
||||||
|
decltype(a + b) add(T a, U b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trailing return types allows this:
|
||||||
|
template <typename T, typename U>
|
||||||
|
auto add(T a, U b) -> decltype(a + b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
In C++14, `decltype(auto)` can be used instead.
|
||||||
|
|
||||||
## C++11 Library Features
|
## C++11 Library Features
|
||||||
|
|
||||||
### std::move
|
### std::move
|
||||||
|
|||||||
33
README.md
33
README.md
@@ -79,6 +79,7 @@ C++11 includes the following new language features:
|
|||||||
- [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)
|
- [ref-qualified member functions](#ref-qualified-member-functions)
|
||||||
|
- [trailing return types](#trailing-return-types)
|
||||||
|
|
||||||
C++11 includes the following new library features:
|
C++11 includes the following new library features:
|
||||||
- [std::move](#stdmove)
|
- [std::move](#stdmove)
|
||||||
@@ -1358,6 +1359,38 @@ std::move(foo).getBar(); // calls `Bar Foo::getBar() &&`
|
|||||||
std::move(foo2).getBar(); // calls `Bar Foo::getBar() const&&`
|
std::move(foo2).getBar(); // calls `Bar Foo::getBar() const&&`
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Trailing return types
|
||||||
|
C++11 allows functions and lambdas an alternative syntax for specifying their return types.
|
||||||
|
```c++
|
||||||
|
int f() {
|
||||||
|
return 123;
|
||||||
|
}
|
||||||
|
// vs.
|
||||||
|
auto f() -> int {
|
||||||
|
return 123;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```c++
|
||||||
|
auto g = []() -> int {
|
||||||
|
return 123;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
This feature is especially useful when certain return types cannot be resolved:
|
||||||
|
```c++
|
||||||
|
// NOTE: This does not compile!
|
||||||
|
template <typename T, typename U>
|
||||||
|
decltype(a + b) add(T a, U b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trailing return types allows this:
|
||||||
|
template <typename T, typename U>
|
||||||
|
auto add(T a, U b) -> decltype(a + b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
In C++14, [decltype(auto)](#decltypeauto) can be used instead.
|
||||||
|
|
||||||
## C++11 Library Features
|
## C++11 Library Features
|
||||||
|
|
||||||
### std::move
|
### std::move
|
||||||
|
|||||||
Reference in New Issue
Block a user