This question already has answers here:
# and ## in macros
(3 answers)
Stringification - how does it work?
(2 answers)
Closed 5 years ago.
What does the '#' symbol do after the second define? And isn't the second line enough? Why the first one?
#define MAKESTRING(n) STRING(n)
#define STRING(n) #n
This is stringize operation, it will produce a string literal from macro parameter, e.g. "n". Two lines are required to allow extra expantion of macro parameter, for example:
// prints __LINE__ (not expanded)
std::cout << STRING(__LINE__) << std::endl;
// prints 42 (line number)
std::cout << MAKESTRING(__LINE__) << std::endl;
Hash symbol takes macro argument into a c-string.
For example
#define MAKESTRING(x) #x
printf(MAKESTRING(text));
will print text
And first line is only alternative name for this macro.
Related
This question already has answers here:
Simplest way to combine two strings at compile time with C++11
(2 answers)
How to concatenate static strings at compile time?
(2 answers)
Concatenate compile-time strings in a template at compile time?
(4 answers)
Closed 8 months ago.
I am attempting to replace as many uses of macros in our code with proper c++17 constructs. How would I replace the following macro with a constexpr or something else?
#define FNAME "first"
#define LNAME "last"
#define NAME FNAME LNAME
const char hello[] = "hello " NAME;
This question already has answers here:
What is the compiler seeing with this macro? [closed]
(4 answers)
Closed 1 year ago.
I came across this macro:
#define STR_ERROR(ecode) case ecode: return #ecode;
What does the #ecode part do?
ecode is an int, and this function returns a const char*.
I'm sure that this has been answered already, but my search-foo has abandoned me. ecode itself is specific to this code. Searching for c++ # gives generic information about macros (as well as some numbered lists relating to C++).
According to cppreference:
# operator before an identifier in the replacement-list runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal
Example from Microsoft Docs
#include <stdio.h>
#define stringer( x ) printf_s( #x "\n" )
int main() {
stringer( In quotes in the printf function call );
stringer( "In quotes when printed to the screen" );
stringer( "This: \" prints an escaped double quote" );
}
Result:
In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quote"
Google-Fu protip: I just searched for C++ macro #, the Google recommended adding operator at the end, and those docs were on the first page.
This question already has answers here:
Rules for C++ string literals escape character
(6 answers)
Closed 6 years ago.
I am trying to create a QStringList containing all punctuation signs.
How can I add the element " into it ?
You can use \ to escape the character ". The code may look like this:
QStringList foo;
foo << "\"";
An other option would be to construct a QString from a char declared between simple quotes ':
foo << QString('"');
Since the constructor isn't declared as explicit in documentation, this should also work with implicit conversion:
foo << '"';
This question already has answers here:
Creating C macro with ## and __LINE__ (token concatenation with positioning macro)
(3 answers)
Closed 6 years ago.
Consider the following macro:
#define CAT(X, Y) X ## Y
#define CMB(A, B) CAT(A, B)
#define SLB_LOGGING_ALGORITHM CMB(Logging, SLB_ALGORITHM)
where SLB_ALGORITHM is a defined pre-processor symbol.
If I just use CAT directly instead of CMB, SLB_ALGORITHM does not get expanded. Why is that the case and how exactly does the indirection help?
## is a string concatenator, so if you call CAT(Logging, SLB_ALGORITHM) from SLB_LOGGING_ALGORITHM macro, this will result in concatenation of string Logging with string SLB_ALGORITHM, that is: LoggingSLB_ALGORITHM which is likely not what you want.
When calling CMB(Logging, SLB_ALGORITHM) from SLB_LOGGING_ALGORITHM macro, instead, preprocessor first expands Logging and SLB_ALGORITHM (call to CMB()) then concatenate the expanded strings (call to CAT()).
To quote this answer:
When you have a macro replacement, the preprocessor will only expand the macros recursively if neither the stringizing operator # nor the token-pasting operator ## are applied to it.
So the preprocessor does not expand a given macro when ## is applied to it. This is why it is exapnded in the CMB(A, B) level but not when directly using CAT(X, Y) .
This question already has answers here:
What are the applications of the ## preprocessor operator and gotchas to consider?
(13 answers)
The ## operator in C
(7 answers)
Closed 9 years ago.
What's the meaning of "##" in the following?
#define CC_SYNTHESIZE(varType, varName, funName)\
protected: varType varName;\
public: inline varType get##funName(void) const { return varName; }\
public: inline void set##funName(varType var){ varName = var; }
The operator ## concatenates two arguments leaving no blank spaces between them:
e.g.
#define glue(a,b) a ## b
glue(c,out) << "test";
This would also be translated into:
cout << "test";
It concatenates tokens without leaving blanks between them. Basically, if you didn't have the ## there
public: inline varType getfunName(void) const { return varName; }\
the precompiler would not replace funName with the parameter value. With ##, get and funName are separate tokens, which means the precompiler can replace funName and then concatenate the results.
This is called token pasting or token concatenation.
The ## (double number sign) operator concatenates two tokens in a macro invocation (text and/or arguments) given in a macro definition.
Take a look here at the official GNU GCC compiler documentation for more information.