Skip to content

Commit c8961c7

Browse files
committed
doc: Add switch on enum example
1 parent 11e3d5e commit c8961c7

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-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

0 commit comments

Comments
 (0)