r/C_Programming • u/orbiteapot • Jan 31 '26
Scoped enums: a weaker version of C++'s enum classes emulated in C23.
Tinkering around with constexpr structs, I came up with this:
#include <stdio.h>
#define concat_aux(a, b) a##b
#define concat(a, b) concat_aux(a, b)
#define have_same_type(obj_1, obj_2) \
_Generic \
( \
(obj_1), \
\
typeof_unqual(obj_2): 1, \
default : 0 \
)
#define decl_enum_scoped(name, underlying_type, ...) \
typedef struct \
{ \
/* Assuming this "trait" is defined somewhere: */ \
static_assert(is_integral(underlying_type)); \
underlying_type int_const_value; \
} name; \
\
typedef struct \
{ \
name __VA_ARGS__ /* optional:*/ __VA_OPT__(,) END; \
} concat(name, _entries); \
constexpr concat(name, _entries) concat(name, _)
// This is necessary for getting the wrapped integer constant:
#define V(e) (e).int_const_value
#define enum_scoped_eq(e_1, e_2) \
({ \
static_assert(have_same_type(e_1, e_2), \
"Enum types do not match"); \
V(e_1) == V(e_2); \
})
// Non GNU C version:
/*
#define enum_scoped_eq(e_1, e_2, ret) \
do { \
static_assert(have_same_type(e_1, e_2), \
"Enum types do not match"); \
*ret = V(e_1) == V(e_2); \
} while (0)
*/
// The last item is just for counting (defaults to zero, if not directly initialized):
decl_enum_scoped(Color, char, RED, GREEN, BLUE, YELLOW) = {0, 1, 2, 3, 4};
decl_enum_scoped(Direction, char, UP, DOWN, LEFT, RIGHT) = {0, 1, 2, 3, 4};
int main(void)
{
Color c_1 = Color_.BLUE;
Color c_2 = Color_.YELLOW;
Direction d_1 = Direction_.UP;
// Would fail: Direction d_2 = Color_.GREEN;
switch( V(c_1) )
{
case V(Color_.RED): puts("Red"); break;
case V(Color_.GREEN): puts("Green"); break;
case V(Color_.BLUE): puts("Blue"); break;
}
// Would fail: enum_scoped_eq(c_1, d_1);
bool res = enum_scoped_eq(c_1, c_2);
res ? puts("true") : puts("false");
return 0;
}It is definitely not as powerful as C++'s enum classes, but I think it is pretty close (at, least, without resorting to absurd macro magic).
Currently, it only works with GCC (version 15.2 - tested here). Clang (21.1.0) has a bug which causes constexpr structs not to yield integer constants. They are already aware of it, though, so it should be fixed at some point.
19
Upvotes
5
u/pjl1967 Jan 31 '26
To have an expression version of
static_assert, you don't need gcc extensions; you can do it in standard C:That, and other macro goodies, can be found here.
1
u/[deleted] Jan 31 '26
Nice implementation.
I wonder how much of this is a problem, especially on modern compilers (where the c23 extensions you use are likely to be implemented)?
For me, given a similar code to your example, I do manage to get warnings (-Wimplicit-enum-enum-cast) on assigning
DIRECTION_UPto a variable of typeenum color, and similarly get the same warning when passing a value of typeenum directionto a function expecting a parameter of typeenun color(and vice versa). Comparing two enum types gives a different warning (-Wenum-compare).Of course, comparing an integer against an enum value gives no such error as the enum value probably decays into an int and thus type info is lost. And warnings are not necessarily a replacement for compiletime errors and static assertions (-Werror nonwithstanding).
But perhaps writing out the naive version suffices to get a good level of "idiot-proofing" for most cases?
The code I tested on clang-21.1.8 (
cc -o enum enum.c):0
u/[deleted] Jan 31 '26
[deleted] — view deleted comment