mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
doc: Add switch on enum example
This commit is contained in:
parent
11e3d5eb1d
commit
c8961c7d9f
1 changed files with 28 additions and 1 deletions
|
@ -87,7 +87,6 @@ code.
|
||||||
- `++i` is preferred over `i++`.
|
- `++i` is preferred over `i++`.
|
||||||
- `nullptr` is preferred over `NULL` or `(void*)0`.
|
- `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.
|
- `static_assert` is preferred over `assert` where possible. Generally; compile-time checking is preferred over run-time checking.
|
||||||
- `enum class` is preferred over `enum` where possible. Scoped enumerations avoid two potential pitfalls/problems with traditional C++ enumerations: implicit conversions to int, and name clashes due to enumerators being exported to the surrounding scope.
|
|
||||||
|
|
||||||
Block style example:
|
Block style example:
|
||||||
```c++
|
```c++
|
||||||
|
@ -563,6 +562,34 @@ class A
|
||||||
- *Rationale*: Easier to understand what is happening, thus easier to spot mistakes, even for those
|
- *Rationale*: Easier to understand what is happening, thus easier to spot mistakes, even for those
|
||||||
that are not language lawyers.
|
that are not language lawyers.
|
||||||
|
|
||||||
|
- Prefer `enum class` (scoped enumerations) over `enum` (traditional enumerations) where possible.
|
||||||
|
|
||||||
|
- *Rationale*: Scoped enumerations avoid two potential pitfalls/problems with traditional C++ enumerations: implicit conversions to `int`, and name clashes due to enumerators being exported to the surrounding scope.
|
||||||
|
|
||||||
|
- `switch` statement on an enumeration example:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
enum class Tabs {
|
||||||
|
INFO,
|
||||||
|
CONSOLE,
|
||||||
|
GRAPH,
|
||||||
|
PEERS
|
||||||
|
};
|
||||||
|
|
||||||
|
int GetInt(Tabs tab)
|
||||||
|
{
|
||||||
|
switch (tab) {
|
||||||
|
case Tabs::INFO: return 0;
|
||||||
|
case Tabs::CONSOLE: return 1;
|
||||||
|
case Tabs::GRAPH: return 2;
|
||||||
|
case Tabs::PEERS: return 3;
|
||||||
|
} // no default case, so the compiler can warn about missing cases
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Rationale*: The comment documents skipping `default:` label, and it complies with `clang-format` rules. The assertion prevents firing of `-Wreturn-type` warning on some compilers.
|
||||||
|
|
||||||
Strings and formatting
|
Strings and formatting
|
||||||
------------------------
|
------------------------
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue