3 ms·
there's absolutely zero compile time safety in your code. If someone copy-pastes for instance case t_int: printf("An integer: %d\n", s.num); break; cas
by jcelerier 9y ago
there's absolutely zero compile time safety in your code. If someone copy-pastes for instance
case t_int: printf("An integer: %d\n", s.num); break;
case t_bool: printf("An boolean: %d\n", s.num); break;
it will just break at runtime, unlike std::variant which prevents this at compile time
- any1 9y agoYes, which is why I would always use the second example for the print function. Edit: In fact it is possible to handle the cases explicitly while enforcing types by having the X-macros call functions in the switch/case and declaring prototypes via the macros: #define X(type, ...) void printSettings_ ## name(type); SETTINGS #undef X void printSettings(const Setting& s) { switch(s.tag) { #define X(type, name) \ case t_ ## type: printSettings_ ## name(s.name); break; SETTINGS #undef X } } Then you define printSettings_... and have them do stuff with the data.