79334204

Date: 2025-01-06 20:08:39
Score: 1
Natty:
Report link

Is there a way to do the if/else chain without starting with an if(0)?

I don't know of a better way to generate an if-else chain with macros. I agree it does look a little hacky.

However, here's another way way to validate that this string is one in the set built from X-Macros that compiles for C++14. Up to your taste if you think its better or not for your codebase.

#include <set>
#include <string>

void validate_string(std::string some_thing) {
    /* Build the set of strings to test against with X-Macros */
    static const std::set<std::string> validate_set = {
    #define X(name) #name,
    MY_LIST
    #undef X
    };

    if (validate_set.find(some_thing) != validate_set.end()){
        /* Then some_thing is one of the compile time constants */
    }
    
}
Reasons:
  • Blacklisted phrase (1): Is there a way
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Is there a
Posted by: GandhiGandhi