Having such a simple C++ code:
#include <iostream>
#define NS NS_Static
#define MAKESTRING(x) #x
namespace NS {
int global_x = 8;
void print_global_x() {
std::cout << MAKESTRING(NS) << "::global_x: " << global_x << std::endl;
}
}
I'h expected to get on output NS_Static::global_x: 8
BUT getting just NS::global_x: 8
. I've asked a similar question earlier and the user Eljay provided me an answer to use such an extra code:
#define NS Static
#define MAKESTRING_HELPER(x) #x
#define MAKESTRING(x) MAKESTRING_HELPER(x)
It finally works but can someone please explain my why my original approach doesn't work?
From https://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html#Argument-Prescan
Macro arguments are completely macro-expanded before they are substituted into a macro body, unless they are stringized or pasted with other tokens.
In your original function:
#define NS NS_Static
#define MAKESTRING(x) #x
The marco argument is stringized, so NS
will not be replaced by NS_Static
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments