I wish to write a macro to output the text of an expression and its value, e.g.
int a = 2;
PRINT(a + 1);
should output
a + 1 = 3
C/C++ Macro string concatenation shows the use of token concatenation. However,
#define PRINT(x) std::cout << x " = " << x << "\n"
or
#define PRINT(x) std::cout << (x) " = " << x << "\n"
gives
error: expected ';' before string constant
while
#define PRINT(x) std::cout << x##" = " << x << "\n"
gives
error: pasting "1" and "" = "" does not give a valid preprocessing token
How can I achieve my aim, please? Thanks!