• Advertisement
Sign in to follow this  

#if question

This topic is 4428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

´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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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;
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement