mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
1.1 KiB
1.1 KiB
Enums
reflect-cpp supports scoped enumerations.
Example:
enum class Color { red, green, blue, yellow };
struct Circle {
float radius;
Color color;
};
const auto circle = Circle{.radius = 2.0, .color = Color::green};
rfl::json::write(circle);
This results in the following JSON string:
{"radius":2.0,"color":"green"}
However, some limitations apply:
- They must be be scoped enumerations.
/// OK - scoped enumeration
enum class Color1 { red, green, blue, yellow };
/// OK - scoped enumeration
enum struct Color2 { red, green, blue, yellow };
/// unsupported - unscoped enumerations
enum Color3 { red, green, blue, yellow };
- You cannot have more than 100 values and if you explicitly assign values, they must be between 0 and 99.
/// OK
enum class Color1 { red, green, blue, yellow };
/// OK
enum struct Color2 { red, green, blue = 50, yellow };
/// unsupported - red is a negative value
enum Color3 { red = -10, green, blue, yellow };
/// unsupported - red is greater than 99
enum Color3 { red = 200, green, blue, yellow };