#if question
´hello Iam just wondering if its possible to compare strings instead of values with #if and if then it possible to put in a macro
some psudo code:
#define MACRO(string) #if string == "hello" (1273) #elif string == "yes" (6782) #else -1 #endif
sadly this doesnt work=(
Thanks for any help
No, that won't really work. For starters, I'm pretty sure that you can't have a #define trying to define another preprocessor directive. Also, it looks like "string" is a variable. The preprocessor stuff is handled before the code is compiled and linked. So, you can't rely on anything that could possibly changed at runtime in a preprocessor directive.
No, and you probably shouldn't use a macro for this. A simple function will do:
int fn(const std::string &s) { if(s == "hello") { return 1273; } else if (s == "yes") { return 6782; } else { return -1; }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement