Skip to content

Commit d0a9e33

Browse files
committed
Merge bitcoin/bitcoin#29469: doc: document preference for list-initialization
eb5d78c doc: document preference for list-initialization (Andrew Toth) Pull request description: Variable initialization is very complex in C++. There seems to be some consensus that when in doubt, use list-initialization. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-list https://www.learncpp.com/cpp-tutorial/variable-assignment-and-initialization/ ACKs for top commit: maflcko: ACK eb5d78c Tree-SHA512: 80d52b062d9e3a0115242779b11385ab583b4c71b27725f63b0a8f82174c04718a57f55a7c1a6df76b405102b175c66abb479589caf363e5d12784e78ad04a93
2 parents edefcd5 + eb5d78c commit d0a9e33

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

doc/developer-notes.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ code.
113113
between integer types, use functional casts such as `int(x)` or `int{x}`
114114
instead of `(int) x`. When casting between more complex types, use `static_cast`.
115115
Use `reinterpret_cast` and `const_cast` as appropriate.
116+
- Prefer [`list initialization ({})`](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-list) where possible.
117+
For example `int x{0};` instead of `int x = 0;` or `int x(0);`
116118

117119
For function calls a namespace should be specified explicitly, unless such functions have been declared within it.
118120
Otherwise, [argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl), also known as ADL, could be
@@ -138,7 +140,7 @@ int main()
138140
139141
Block style example:
140142
```c++
141-
int g_count = 0;
143+
int g_count{0};
142144
143145
namespace foo {
144146
class Class
@@ -150,7 +152,7 @@ public:
150152
{
151153
// Comment summarising what this section of code does
152154
for (int i = 0; i < n; ++i) {
153-
int total_sum = 0;
155+
int total_sum{0};
154156
// When something fails, return early
155157
if (!Something()) return false;
156158
...

0 commit comments

Comments
 (0)