Skip to content

Commit dcc6408

Browse files
committed
Merge #17134: doc: Add switch on enum example to developer notes
c8961c7 doc: Add switch on enum example (Hennadii Stepanov) 11e3d5e util: Add AllowShortCaseLabelsOnASingleLine option (Hennadii Stepanov) Pull request description: This PR documents a recurring issue: - #15938 - #17105 ACKs for top commit: laanwj: Seems like good advice to me. ACK c8961c7 practicalswift: ACK c8961c7 promag: ACK c8961c7, no excuse now, thanks! Tree-SHA512: 530da5117094ed1bfaa6e447089521bd2c86b0742758dbacec4e4f934dc07b0e24f15a1448c4d58e49905e8fd3797d87bcae5669a346d33ed4c2878a04891699
2 parents b33c03b + c8961c7 commit dcc6408

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

doc/developer-notes.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ code.
8787
- `++i` is preferred over `i++`.
8888
- `nullptr` is preferred over `NULL` or `(void*)0`.
8989
- `static_assert` is preferred over `assert` where possible. Generally; compile-time checking is preferred over run-time checking.
90-
- `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.
9190

9291
Block style example:
9392
```c++
@@ -563,6 +562,34 @@ class A
563562
- *Rationale*: Easier to understand what is happening, thus easier to spot mistakes, even for those
564563
that are not language lawyers.
565564
565+
- Prefer `enum class` (scoped enumerations) over `enum` (traditional enumerations) where possible.
566+
567+
- *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.
568+
569+
- `switch` statement on an enumeration example:
570+
571+
```cpp
572+
enum class Tabs {
573+
INFO,
574+
CONSOLE,
575+
GRAPH,
576+
PEERS
577+
};
578+
579+
int GetInt(Tabs tab)
580+
{
581+
switch (tab) {
582+
case Tabs::INFO: return 0;
583+
case Tabs::CONSOLE: return 1;
584+
case Tabs::GRAPH: return 2;
585+
case Tabs::PEERS: return 3;
586+
} // no default case, so the compiler can warn about missing cases
587+
assert(false);
588+
}
589+
```
590+
591+
*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.
592+
566593
Strings and formatting
567594
------------------------
568595

src/.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ AlignEscapedNewlinesLeft: true
55
AlignTrailingComments: true
66
AllowAllParametersOfDeclarationOnNextLine: false
77
AllowShortBlocksOnASingleLine: false
8+
AllowShortCaseLabelsOnASingleLine: true
89
AllowShortFunctionsOnASingleLine: All
910
AllowShortIfStatementsOnASingleLine: true
1011
AllowShortLoopsOnASingleLine: false

0 commit comments

Comments
 (0)