diff options
author | fanquake <fanquake@gmail.com> | 2023-02-08 10:36:35 +0000 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2023-02-08 10:36:43 +0000 |
commit | 8d69b614cc59fc94dbbde97cadbd2ec1749bd3fd (patch) | |
tree | b06bc0d41aa703f9e5704e561ff332228547ddde | |
parent | 1bcabe6f2ac8a64afdf14025eefb606ed07dcf1b (diff) | |
parent | 75347236f212f327a5bba10d8a900cc58ebe5de0 (diff) |
Merge bitcoin/bitcoin#23810: docs: avoid C-style casts; use modern C++ casts
75347236f212f327a5bba10d8a900cc58ebe5de0 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
-rw-r--r-- | doc/developer-notes.md | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/doc/developer-notes.md b/doc/developer-notes.md index 00c68911ef..e2e54e13d3 100644 --- a/doc/developer-notes.md +++ b/doc/developer-notes.md @@ -109,6 +109,10 @@ code. - `++i` is preferred over `i++`. - `nullptr` is preferred over `NULL` or `(void*)0`. - `static_assert` is preferred over `assert` where possible. Generally; compile-time checking is preferred over run-time checking. + - Use a named cast or functional cast, not a C-Style cast. When casting + between integer types, use functional casts such as `int(x)` or `int{x}` + instead of `(int) x`. When casting between more complex types, use static_cast. + Use reinterpret_cast and const_cast as appropriate. For function calls a namespace should be specified explicitly, unless such functions have been declared within it. Otherwise, [argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl), also known as ADL, could be |