|
| 1 | +<div align=right> |
| 2 | + |
| 3 | + 🌎 [中文] | [English] |
| 4 | +</div> |
| 5 | + |
| 6 | +[中文]: ../../cpp14/03-return-type-deduction.html |
| 7 | +[English]: ./03-return-type-deduction.html |
| 8 | + |
| 9 | +# Return Type Deduction |
| 10 | + |
| 11 | +C++14 allows functions to use `auto` as a return type without trailing `-> decltype(...)` |
| 12 | + |
| 13 | +| Book | Video | Code | X | |
| 14 | +| --- | --- | --- | --- | |
| 15 | +| [cppreference-auto](https://en.cppreference.com/w/cpp/language/auto) / [markdown](https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp14/03-return-type-deduction.md) | [Video Explanation]() | [Exercise Code](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/03-return-type-deduction-0.cpp) | | |
| 16 | + |
| 17 | + |
| 18 | +**Why introduced?** |
| 19 | + |
| 20 | +- C++11 required `-> decltype(...)` trailing return for `auto` functions |
| 21 | +- Many simple functions don't need an explicit return type |
| 22 | + |
| 23 | +## I. Basic Usage and Scenarios |
| 24 | + |
| 25 | +### auto return — plain functions |
| 26 | + |
| 27 | +```cpp |
| 28 | +auto add(int a, int b) { // deduced as int |
| 29 | + return a + b; |
| 30 | +} |
| 31 | +``` |
| 32 | +
|
| 33 | +### auto return — template functions |
| 34 | +
|
| 35 | +```cpp |
| 36 | +template <typename T1, typename T2> |
| 37 | +auto multiply(T1 a, T2 b) { // C++14: no -> decltype needed |
| 38 | + return a * b; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## II. Real-World Case — auto Return Deduction in the STL |
| 43 | + |
| 44 | +> The MSVC STL uses `auto` return types extensively to simplify template function signatures. The examples below cite the vendored [MSVC STL](https://github.com/mcpp-community/d2mcpp/tree/main/msvc-stl) (source: [`msvc-stl/stl/inc/xutility`](https://github.com/mcpp-community/d2mcpp/blob/main/msvc-stl/stl/inc/xutility#L1129-L1132)); `_NODISCARD` / `constexpr` are library-internal annotations |
| 45 | +
|
| 46 | +### _To_unsigned_like — auto Return Simplifies Template Functions |
| 47 | + |
| 48 | +```cpp |
| 49 | +// MSVC STL · msvc-stl/stl/inc/xutility (abridged) |
| 50 | +template <_Integer_like _Ty> |
| 51 | +_NODISCARD constexpr auto _To_unsigned_like(const _Ty _Value) noexcept { |
| 52 | + return static_cast<_Make_unsigned_like_t<_Ty>>(_Value); |
| 53 | +} |
| 54 | +``` |
| 55 | +
|
| 56 | +The return type is `_Make_unsigned_like_t<_Ty>` — without `auto`, the signature would be `_Make_unsigned_like_t<_Ty> _To_unsigned_like(...)`, with the return type longer than the function name. `auto` keeps the signature clean while the return statement naturally expresses the type |
| 57 | +
|
| 58 | +## III. Notes |
| 59 | +
|
| 60 | +### auto return requires consistent types |
| 61 | +
|
| 62 | +```cpp |
| 63 | +auto bad(int x) { |
| 64 | + if (x > 0) return 1; // int |
| 65 | + else return 2.0; // double → error! |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Recursion requires explicit return type |
| 70 | + |
| 71 | +```cpp |
| 72 | +auto factorial(int n) { // error: recursive |
| 73 | + return n <= 1 ? 1 : n * factorial(n - 1); |
| 74 | +} |
| 75 | +``` |
| 76 | +
|
| 77 | +### auto return strips references |
| 78 | +
|
| 79 | +Use `decltype(auto)` when you need to preserve references (see next chapter) |
| 80 | +
|
| 81 | +## IV. Exercise Code |
| 82 | +
|
| 83 | +### Exercise Topics |
| 84 | +
|
| 85 | +- 0 - [auto Return Type Deduction](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/03-return-type-deduction-0.cpp) |
| 86 | +
|
| 87 | +### Auto-Checker Command |
| 88 | +
|
| 89 | +``` |
| 90 | +d2x checker return-type-deduction |
| 91 | +``` |
| 92 | +
|
| 93 | +## V. Other |
| 94 | +
|
| 95 | +- [Discussion Forum](https://forum.d2learn.org/category/20) |
| 96 | +- [d2mcpp Tutorial Repository](https://github.com/mcpp-community/d2mcpp) |
| 97 | +- [Tutorial Video List](https://space.bilibili.com/65858958/lists/5208246) |
| 98 | +- [Tutorial Support Tool - xlings](https://github.com/openxlings/xlings) |
0 commit comments