Can you write preprocessor directives to return you a std::string or char*?
For example: In case of integers:
#define square(x) (x*x)
int main()
{
int x = square(5);
}
I’m looking to do the same but with strings like a switch-case pattern. if pass 1 it should return “One” and 2 for “Two” so on..
You don’t want to do this with macros in C++; a function is fine:
Similarly, that square should be a function:
(Though in practice square isn’t very useful, you’d just multiply directly.)
As a curiosity, though I wouldn’t recommend it in this case (the above function is fine), a template meta-programming equivalent would be:
Note that the primary way the TMP example fails is you have to use compile-time constants instead of any int.