Skip to content

Commit

Permalink
Update ES.23 to allow = initialiization (isocpp#1416)
Browse files Browse the repository at this point in the history
* Update ES.23 to allow = initialiization

* Silencing Travis

* Changed title back to original, it's fine

* Add note about explicit
  • Loading branch information
hsutter authored May 2, 2019
1 parent a9242c8 commit 9948bdc
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions CppCoreGuidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -10647,27 +10647,35 @@ For initializers of moderate complexity, including for `const` variables, consid
* Flag declarations with default initialization that are assigned to before they are first read.
* Flag any complicated computation after an uninitialized variable and before its use.

### <a name="Res-list"></a>ES.23: Prefer the `{}` initializer syntax
### <a name="Res-list"></a>ES.23: Prefer the `{}`-initializer syntax

##### Reason

The rules for `{}` initialization are simpler, more general, less ambiguous, and safer than for other forms of initialization.
Prefer `{}`. The rules for `{}` initialization are simpler, more general, less ambiguous, and safer than for other forms of initialization.

Use `=` only when you are sure that there can be no narrowing conversions. For built-in arithmetic types, use `=` only with `auto`.

Avoid `()` initialization, which allows parsing ambiguities.

##### Example

int x {f(99)};
int y = x;
vector<int> v = {1, 2, 3, 4, 5, 6};

##### Exception

For containers, there is a tradition for using `{...}` for a list of elements and `(...)` for sizes:

vector<int> v1(10); // vector of 10 elements with the default value 0
vector<int> v2 {10}; // vector of 1 element with the value 10
vector<int> v2{10}; // vector of 1 element with the value 10

vector<int> v3(1, 2); // vector of 1 element with the value 2
vector<int> v4{1, 2}; // vector of 2 element with the values 1 and 2

##### Note

`{}`-initializers do not allow narrowing conversions (and that is usually a good thing).
`{}`-initializers do not allow narrowing conversions (and that is usually a good thing) and allow explicit constructors (which is fine, we're intentionally initializing a new variable).

##### Example

Expand All @@ -10677,7 +10685,7 @@ For containers, there is a tradition for using `{...}` for a list of elements an

##### Note

`{}` initialization can be used for all initialization; other forms of initialization can't:
`{}` initialization can be used for nearly all initialization; other forms of initialization can't:

auto p = new vector<int> {1, 2, 3, 4, 5}; // initialized vector
D::D(int a, int b) :m{a, b} { // member initializer (e.g., m might be a pair)
Expand All @@ -10698,7 +10706,7 @@ Initialization of a variable declared using `auto` with a single value, e.g., `{
The C++17 rules are somewhat less surprising:

auto x1 {7}; // x1 is an int with the value 7
auto x2 = {7}; // x2 is an initializer_list<int> with an element 7
auto x2 = {7}; // x2 is an initializer_list<int> with an element 7

auto x11 {7, 8}; // error: two initializers
auto x22 = {7, 8}; // x22 is an initializer_list<int> with elements 7 and 8
Expand All @@ -10720,10 +10728,6 @@ Like the distinction between copy-initialization and direct-initialization itsel

Use plain `{}`-initialization unless you specifically want to disable explicit constructors.

##### Note

Old habits die hard, so this rule is hard to apply consistently, especially as there are so many cases where `=` is innocent.

##### Example

template<typename T>
Expand All @@ -10741,10 +10745,8 @@ Old habits die hard, so this rule is hard to apply consistently, especially as t

##### Enforcement

Tricky.

* Don't flag uses of `=` for simple initializers.
* Look for `=` after `auto` has been seen.
* Flag uses of `=` to initialize arithmetic types where narrowing occurs.
* Flag uses of `()` initialization syntax that are actually declarations. (Many compilers should warn on this already.)

### <a name="Res-unique"></a>ES.24: Use a `unique_ptr<T>` to hold pointers

Expand Down

0 comments on commit 9948bdc

Please sign in to comment.