mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:34:45 +01:00
Be a little more prescriptive about {} initialization in style. (#4009)
Following discussions around #3958, try to provide more specific semantics. Note we currently don't follow this everywhere, particularly in AddInst calls, but the intent is to shift. Per discussion, designated initializers are preferred when possible. And the `google-readability-casting` diagnostics are poor, but with this may primarily flag cases which should be using a different constructor syntax, so are just a rocky way to get there. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
co-authored by
Richard Smith
parent
7e81c1710e
commit
cdc0ca14ec
@@ -124,17 +124,27 @@ these.
|
||||
- An exception is made for functions like `std::swap` that are
|
||||
intentionally called using ADL. This pattern should be written as
|
||||
`{ using std::swap; swap(thing1, thing2); }`.
|
||||
- Follow the rules for initialization outlined in
|
||||
[Abseil's tip #88](https://abseil.io/tips/88#best-practices-for-initialization).
|
||||
To summarize, omitting some details from the article:
|
||||
- For initialization:
|
||||
- Use assignment syntax (`=`) when initializing directly with the intended
|
||||
value (or with a braced initializer directly specifying that value).
|
||||
- Use the traditional constructor syntax (with parentheses) when the
|
||||
initialization is performing some active logic, rather than simply
|
||||
composing values together.
|
||||
- Use `{}` initialization without the `=` only if the above options don't
|
||||
- Prefer braced initialization for aggregate initialization, such as
|
||||
structs, pairs, and initializer lists.
|
||||
- Use designated initializers (`{.a = 1}`) when possible for structs,
|
||||
but not for pairs or tuples. Prefer to only include the typename
|
||||
when required to compile (`WizType{.a = 1}`). This is analogous to
|
||||
how structs and tuples would be written in Carbon code.
|
||||
- Avoid braced initialization for types that define a constructor,
|
||||
except as an initializer list
|
||||
(`llvm::SmallVector<int> v = {0, 1};`), `std::pair`, or
|
||||
`std::tuple`. Never use it with `auto` (`auto a = {0, 1}`).
|
||||
- Prefer parenthesized initialization (`FooType foo(10);`) in most other
|
||||
cases.
|
||||
- Braced initialization without `=` (`BarType bar{10}`) should be treated
|
||||
as a fallback, preferred only when other constructor syntax doesn't
|
||||
compile.
|
||||
- Never mix `{}` initialization and `auto`.
|
||||
- Some additional commentary is in
|
||||
[Abseil's tip #88](https://abseil.io/tips/88#best-practices-for-initialization),
|
||||
although these guidelines differ slightly.
|
||||
- Always mark constructors `explicit` unless there's a specific reason to
|
||||
support implicit or `{}` initialization.
|
||||
- Always use braces for conditional, `switch`, and loop statements, even when
|
||||
|
||||
Reference in New Issue
Block a user