I have a C++ macro with a syntax that I have never seen before:
#define ASSERT(a) \
if (! (a)) \
{ \
std::string c; \
c += " test(" #a ")";
}
Couold you please explain me the use of # in here? I wanted to put the macro in a static function but before I would like to fully understand what it does.
Thanks
The use of #
in a macro means that the macro argument will be wrapped in quotes ""
:
#define FOOBAR(x) #x
int main (int argc, char *argv[])
{
std::cout << FOOBAR(hello world what's up?) << std::endl;
}
output
hello world what's up?
In the below we display the contents of foo.cpp, and then what the file will look like after the pre-processor has run:
:/tmp% cat foo.cpp
#define STR(X) #X
STR (hello world);
...
:/tmp% g++ -E foo.cpp # only run the preprocessor
# 1 "foo.cpp"
# 1 "<command-line>"
# 1 "foo.cpp"
"hello world";
Check out the following link to a entry in the cpp
(C Pre Processor) documentation:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With