In C++17, what do `auto a{1};`, `auto b = {1};` and `auto c{1, 2};` deduce, and what changed from C++14?
answer
- form of initialization decides
- copy-list gives initializer_list
- direct-list: single element only
- C++17 changed the one-element case
basics
~10 sSince C++17, auto a{1} deduces int, auto b = {1} deduces std::initializer_list<int>, and auto c{1, 2} is ill-formed. In C++14 the standard deduced std::initializer_list<int> for both brace forms.
solid answer
~30 sWith braces, `auto` deduction depends on the initialization form. Copy-list-initialization, `auto b = {1};` or `= {1, 2}`, deduces `std::initializer_list<U>`, and every element must yield the same `U`, so `{1, 2.0}` is ill-formed. Direct-list-initialization without `=` requires exactly one element and deduces from it: since C++17 `auto a{1};` is `int` and `auto c{1, 2};` is an error. C++14's wording deduced `std::initializer_list<int>` for `auto a{1};` too, although Apple clang 21 applies the C++17 rule even under `-std=c++14`. A deduced list is a view of a const array that also needs `<initializer_list>` declared, and it is rarely what was meant.
code
cpp · 14 lines#include <initializer_list>
#include <type_traits>
int main() {
auto a{1}; // int (C++17 rule)
auto b = {1}; // std::initializer_list<int>
auto e = {1, 2}; // std::initializer_list<int>
static_assert(std::is_same_v<decltype(a), int>);
static_assert(std::is_same_v<decltype(b), std::initializer_list<int>>);
static_assert(std::is_same_v<decltype(e), std::initializer_list<int>>);
// auto c{1, 2}; // error: initializer for variable 'c' with type 'auto' contains multiple expressions
// auto d = {1, 2.0};// error: deduced conflicting types ('int' vs 'double') for initializer list element type
(void)a; (void)b; (void)e;
}go deeper
Recall the C++17 results: auto x{1} is int, auto x = {1} is std::initializer_list<int>, and auto x{1, 2} does not compile.
Explain copy-list versus direct-list deduction, the same-type requirement for list elements, and what C++17 changed from C++14.
Check code built across -std levels for brace deduction that compilers treat differently from the C++14 text, and replace deduced lists with explicit types.
Decide one team rule for auto with braces, typically auto x = value only, and enforce it in review or tooling.
## Four lines, four outcomes `auto` combined with braces is the one place where deduction depends on the **form of initialization**, not only on the initializer's type. Since C++17: | Declaration | Result | |---|---| | `auto a{1};` | `int` | | `auto b = {1};` | `std::initializer_list<int>` | | `auto c{1, 2};` | ill-formed: more than one element in direct-list-initialization | | `auto d = {1, 2.0};` | ill-formed: `int` and `double` conflict | | `auto e = {1, 2};` | `std::initializer_list<int>` | The draft's declarations clause states the two halves: for **copy-list-initialization** (`= { ... }`) the placeholder is deduced as `std::initializer_list<U>`, with all elements required to deduce the same `U`; for **direct-list-initialization** (`{ ... }` with no `=`) the braces must contain a single element, and the type is deduced from that element. ## What C++17 changed C++14 deduced `std::initializer_list<int>` for `auto a{1};` as well. The C++17 compatibility annex records the change: ```cpp auto x1{1}; // was std::initializer_list<int>, now int auto x2{1, 2}; // was std::initializer_list<int>, now ill-formed ``` The motivation was that `auto x{1};` read as "an int initialized with braces", and people who adopted uniform brace initialization were getting a one-element list. Apple clang 21 applies the new rule even with `-std=c++14`, deducing `int` for `auto x{1};`, so a codebase built in C++14 mode may already rely on the C++17 behaviour without knowing it. ## The diagnostics Apple clang 21 reports the two ill-formed lines as: - `auto c{1, 2};`: "initializer for variable 'c' with type 'auto' contains multiple expressions"; - `auto d = {1, 2.0};`: "deduced conflicting types ('int' vs 'double') for initializer list element type". There is a third, less obvious requirement: `std::initializer_list` is "not predefined". If no declaration of it is reachable, `auto e = {1, 2};` is ill-formed even though the code never names the type; clang says "cannot deduce type of initializer list because std::initializer_list was not found; include <initializer_list>". ## Why the list form bites A deduced `std::initializer_list` is rarely what code meant: - it is a lightweight **view of a const array**; its elements cannot be modified or moved from; - it changes **overload resolution** when passed on: a constructor taking `std::initializer_list` is preferred for brace-initialized arguments, which is the source of the `std::vector<int>{10, 20}` versus `(10, 20)` difference covered with brace initialization; - copying the `initializer_list` object does not copy the elements, so storing one beyond the expression that created its backing array leads to dangling. ## Related deduction contexts The same rules reach further than local variables: 1. A function with a deduced return type cannot `return {1, 2};`: a braced list is not an expression, so there is nothing to deduce from. 2. `decltype(auto) x = {1, 2};` is an error for the same reason; `decltype(auto)` needs an expression. 3. A range-for over `{1, 2, 3}` works: the range is deduced as `std::initializer_list<int>`, which is exactly what is wanted there. 4. A lambda's init-capture `[v = {1, 2}]` is deduced like `auto v = {1, 2}`, a list. ## Moving a codebase across standards When a project raises its `-std` level, brace deduction is one of the few places where existing code can change type without failing to compile: 1. `auto x{1};` written for C++11 or C++14 meant a one-element `std::initializer_list<int>` by the standard's text; under C++17 it is an `int`, so code calling `x.size()` stops compiling, while code that used `x` as a value starts working. 2. `auto x{1, 2};` stops compiling under C++17, which is the safe direction: the error marks the line to rewrite as `auto x = {1, 2};` or as an explicit container. 3. Because Apple clang 21 already applies the C++17 rule in C++14 mode, the observed behaviour may not change at all on that compiler; checking with `static_assert` on `decltype` is more reliable than reading the standard of the day. ## A rule a team can apply - Write `auto x = value;` for deduced locals; reserve braces for non-deduced types where narrowing protection matters (`int n{len};`). - Write `std::initializer_list<int>` explicitly in the rare case a list is wanted, so a reader does not have to remember which form deduces what. - In a code review, `auto` immediately followed by braces is worth a second look, and `auto x = { ... }` almost always.
- Why can a function with an `auto` return type not `return {1, 2};`?Return type deduction deduces from the operand of `return`, and a braced-init-list is not an expression, so there is no type to deduce; the program is ill-formed. Name the type, as in `return std::vector<int>{1, 2};`, or declare the return type explicitly.
- Does `auto b = {1};` compile without including any header?No. `std::initializer_list` is not predefined, and the draft makes a use of it ill-formed when no declaration is reachable, even an implicit use by `auto` deduction. Clang reports that it cannot deduce the type and suggests including `<initializer_list>`; most standard headers already provide it indirectly.
- auto x{1} and auto x = {1} deduce the same type.
- auto x{1, 2} deduces std::initializer_list<int> in C++17.
- auto x = {1, 2.0} deduces std::initializer_list<double>.
- Brace initialization never affects what auto deduces.
- A deduced std::initializer_list owns modifiable copies of its elements.
Assumes C++17 or later; C++14's wording deduced std::initializer_list<int> for auto x{1}, which C++17 changed to int.