Skip to content

Commit 8d69b61

Browse files
committed
Merge bitcoin/bitcoin#23810: docs: avoid C-style casts; use modern C++ casts
7534723 docs: document c-style cast prohibition (Pasta) Pull request description: In the words of practicalswift: ``` A C-style cast is equivalent to try casting in the following order: const_cast(...) static_cast(...) const_cast(static_cast(...)) reinterpret_cast(...) const_cast(reinterpret_cast(...)) By using static_cast<T>(...) explicitly we avoid the possibility of an unintentional and dangerous reinterpret_cast. Furthermore static_cast<T>(...) allows for easier grepping of casts. For a more thorough discussion, see "ES.49: If you must use a cast, use a named cast" in the C++ Core Guidelines (Stroustrup & Sutter). ``` Modern tooling, specifically `-Wold-style-cast` can enable us to enforce never using C-style casts. I believe this is especially important due to the number of C-style casts the codebase is currently being used as a reinterpret_cast. reinterpret_casts are especially dangerous, and should never be done via C-style casts. Update the docs to suggest the use of named cast or functional casts. Top commit has no ACKs. Tree-SHA512: 29a98de396f0c78e32d8a1831319162203c4405a670da5add5da956fcc7df200a1cec162ef1cfac4ddfb02714b66406081d40ed435c7f0f28581cfa24d94fac1
2 parents 1bcabe6 + 7534723 commit 8d69b61

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

doc/developer-notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ code.
109109
- `++i` is preferred over `i++`.
110110
- `nullptr` is preferred over `NULL` or `(void*)0`.
111111
- `static_assert` is preferred over `assert` where possible. Generally; compile-time checking is preferred over run-time checking.
112+
- Use a named cast or functional cast, not a C-Style cast. When casting
113+
between integer types, use functional casts such as `int(x)` or `int{x}`
114+
instead of `(int) x`. When casting between more complex types, use static_cast.
115+
Use reinterpret_cast and const_cast as appropriate.
112116

113117
For function calls a namespace should be specified explicitly, unless such functions have been declared within it.
114118
Otherwise, [argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl), also known as ADL, could be

0 commit comments

Comments
 (0)