forked from quicknir/wise_enum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.x.cpp
More file actions
45 lines (34 loc) · 1.23 KB
/
Copy pathexample.x.cpp
File metadata and controls
45 lines (34 loc) · 1.23 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
#if __cplusplus == 201402L
#include <experimental/optional>
#define WISE_ENUM_OPTIONAL std::experimental::optional
#endif
#include "wise_enum.h"
#include <cassert>
#include <iostream>
// Equivalent to enum Color {GREEN = 2, RED};
WISE_ENUM(Color, (GREEN, 2), RED)
int main() {
// Number of enumerations:
static_assert(wise_enum::size<Color> == 2, "");
std::cerr << "Number of enumerators: " << wise_enum::size<Color> << "\n";
// Iterate over enums
std::cerr << "Enum values and names:\n";
for (auto e : wise_enum::range<Color>) {
std::cerr << static_cast<int>(e.value) << " " << e.name << "\n";
}
std::cerr << "\n";
// Convert any enum to a string
std::cerr << wise_enum::to_string(Color::RED) << "\n";
// Convert any string to an optional enum
auto x1 = wise_enum::from_string<Color>("GREEN");
auto x2 = wise_enum::from_string<Color>("Greeeeeeen");
assert(x1.value() == Color::GREEN);
assert(!x2);
// Everything is constexpr, and a type trait is made available for easy use in
// enable_if/tag dispatch
static_assert(wise_enum::is_wise_enum_v<Color>, "");
static_assert(!wise_enum::is_wise_enum_v<int>, "");
enum flub { blub, glub };
static_assert(!wise_enum::is_wise_enum_v<flub>, "");
return 0;
}