|
| 1 | +# `auto`-type-deduction |
| 2 | + |
| 3 | +The type deduction rules for auto are largely the same as for templates. |
| 4 | +In an expression like this: |
| 5 | + |
| 6 | +const auto x = 5; |
| 7 | + |
| 8 | +auto takes the role of T in template type deduction and the _type |
| 9 | +specifier_ (const auto) takes the role of the parameter type Type. |
| 10 | + |
| 11 | +Thus all these cases will be the same as for template type deduction: |
| 12 | + |
| 13 | + auto x = 5; // auto deduces to int |
| 14 | + |
| 15 | + const auto& rx = x; // auto deduces to int, the specifier to const int& |
| 16 | + |
| 17 | + auto&& urx1 = x; // auto deduces to int&, the specifier too |
| 18 | + |
| 19 | + auto&& urx2 = 27; // auto decues to int, the specifier to int&& |
| 20 | + |
| 21 | + auto* px = &x; // auto deduces to int, the specifier to int* |
| 22 | + |
| 23 | + int arr [2] = {1, 2}; |
| 24 | + |
| 25 | + auto a = arr; // auto deduces to int* (array decays) |
| 26 | + |
| 27 | + auto& ra = arr; // auto deduces to int (&) [2] |
| 28 | + |
| 29 | + void f(int, std::string); |
| 30 | + |
| 31 | + auto g = f; // auto deduces to void (*) (int, std::string) |
| 32 | + |
| 33 | + auto& g = f; // auto deduces to void (&) (int, std::string) |
| 34 | + |
| 35 | +There is __one__ important difference, regarding initializer-lists: |
| 36 | +template-type deduction for a brace-initialized sequence fails, while |
| 37 | +auto will deduce it as an std::initializer_list<T> after T has been |
| 38 | +deduced from the elements in the list (if possible, i.e. if all elements |
| 39 | +in the list have a single type, else fails too). Examples: |
| 40 | + |
| 41 | + auto a = 5; // auto -> int |
| 42 | + auto b(5); // auto -> int |
| 43 | + auto c = {5}; // auto -> std::initializer_list<int> |
| 44 | + auto d{5}; // auto -> std::initializer_list<int> or int -- CAREFUL! |
| 45 | + auto e = {5, 1.0}; // fails (not only one type) |
| 46 | + |
| 47 | +Special care must be taken for d. As of now, this does initialize an |
| 48 | +std::initializer_list. To ensure uniform initialization capabilities, |
| 49 | +however, this will _very soon_ change to initialize an integer and many |
| 50 | +compilers already implement it while others warn about it. So if you |
| 51 | +want to store an initializer list in an auto variable, use = {...} and |
| 52 | +not {...}. |
| 53 | + |
| 54 | +Why does auto differ from template type deduction for initializer lists? |
| 55 | +Because template type deduction never implicitly converts anything. |
| 56 | +Consider this case: |
| 57 | + |
| 58 | + template<typename T> |
| 59 | + void assign(T& target, const T& source); |
| 60 | + |
| 61 | + std::vector<int> v; |
| 62 | + |
| 63 | + assign(v, {1, 2, 3}); |
| 64 | + |
| 65 | +In this case, were {1, 2, 3} automatically deduces to be |
| 66 | +std::initializer_list<int>, this method call would fail because the |
| 67 | +types for T would differ. Without this automatic deduction (currently in |
| 68 | +place), T is deduces to be std::vector<int> and {1, 2, 3} initializes |
| 69 | +the source vector correctly. |
| 70 | + |
| 71 | +The fact that auto automatically deduces a brace-list as an |
| 72 | +std::initializer_list is a polarizing topic and there exist proposals |
| 73 | +also to ban it. |
| 74 | + |
| 75 | +A last note about auto in lambdas or return types: there, template type |
| 76 | +deduction rules apply and not auto type deduction rules: |
| 77 | + |
| 78 | + template<typename Function> |
| 79 | + void f(Function function) |
| 80 | + { |
| 81 | + // Fails, template type deduction does not automatically |
| 82 | + // deduce this as std::initializer_list |
| 83 | + function({1, 2, 3}); |
| 84 | + } |
| 85 | + |
| 86 | + auto g() |
| 87 | + { |
| 88 | + // Also fails for same reasons as above. |
| 89 | + return {1, 2, 3}; |
| 90 | + } |
| 91 | + |
| 92 | + // Initializes a valid std::initializer_list |
| 93 | + auto list = {1, 2, 3}; |
0 commit comments