-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.cpp
More file actions
112 lines (79 loc) · 3.16 KB
/
Copy pathenum.cpp
File metadata and controls
112 lines (79 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#pragma clang diagnostic ignored "-Wunused-function"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
namespace {
// enums
// -
//
// Key notes:
// - underlying types can be:
// Any integral type:
// - bool, char, signed char, unsigned char
// - short, unsigned short
// - int, unsigned int
// - long, unsigned long
// - long long, unsigned long long
TEST_CASE("enu-1") {
//// plain enum — C style, leaks names into scope
enum Color { Red, Green, Blue };
// enum Status { Red, Ok }; // ❌ Red redefined — name collision
int x = Red; // ✓ implicit int conversion — unsafe
if (x == Green) {} // ✓ compares int to int — no type check
//// enum class — scoped, typed, safe (C++11)
// - prefer enum class over plain enum — scoped, no implicit int conversion
enum class Color_ { Red, Green, Blue };
enum class Status { Active, Inactive }; // ✓ no collision
Color_ c_ = Color_::Red; // must qualify ✓
// int x_ = Color_::Red; // ❌ no implicit conversion ✓
int x_ = static_cast<int>(Color::Red); // ✓ explicit cast required
//// underlying type — default int
// - specify underlying type for ABI stability and memory control
enum class Color__ : uint8_t { Red, Green, Blue }; // saves memory
enum class Flags : uint32_t { None=0, A=1, B=2 }; // explicit values
enum OldStyle : short { X, Y }; // plain enum with type
//// explicit values
enum class HttpStatus : int {
Ok = 200,
NotFound = 404,
ServerError = 500
};
HttpStatus s = HttpStatus::Ok;
static_cast<int>(s); // 200 ✓
//// std::to_underlying — C++23
// - std::to_underlying (C++23) cleaner than static_cast<int>
auto val = std::to_underlying(HttpStatus::Ok); // 200 ✓
auto val_ = static_cast<int>(HttpStatus::Ok); // C++11 ✓
//// check value validity
bool valid = (s == HttpStatus::Ok ||
s == HttpStatus::NotFound ||
s == HttpStatus::ServerError);
//// switch
switch (s) {
using enum HttpStatus; // C++20 — drop prefix ✓
case Ok: break;
case NotFound: break;
case ServerError: break;
}
//// iterate — no built-in, common workaround
enum class Dir { North, South, East, West, COUNT };
for (int i = 0; i < static_cast<int>(Dir::COUNT); ++i) {
Dir d = static_cast<Dir>(i);
}
//// bitflags — plain enum or uint
enum Perms : uint32_t {
None = 0,
Read = 1 << 0, // 0001
Write = 1 << 1, // 0010
Execute = 1 << 2, // 0100
};
uint32_t p = Read | Write; // ✓
bool canRead = p & Read; // ✓
//// forward declare enum class
enum class Color1 : int; // ✓ must specify underlying type
void fn(Color1 c); // use before definition ✓
}
}