3 ms·
What is wrong with the following? Do you want type more type safety, e.g. linking the enumeration constants to the array? enum { FIRST, SECOND, THIRD }; st
by uecker 2y ago
What is wrong with the following? Do you want type more type safety, e.g. linking the enumeration constants to the array?
enum { FIRST, SECOND, THIRD };
struct { char *name; int value; } table[] = {
[FIRST] = { .name = "FIRST", .value = 0 },
[SECOND] = { .name = "SECOND", .value = 1 },
[THIRD] = { .name = "THIRD", .value = 3 },
};
- gpderetta 2y agoYou still need generic code to link the enum type to its format string, for example for use in std::format or std::ostream. Nothing really difficult, normally all of this is done by hand, which is tedious and error prone, or incorporated in a macro. It would just be nice if there was a standard provided turn-key solution to the problem.
- jcelerier 2y agoI have yet to see a project where there have never been bugs due to someone forgetting to update the metadata attached with the enum when there's a change