Because the evaluation order of #
and ##
are not specified, the results of using them both in the same macro could be
unpredictable. Therefore macros should contain at most once instance of either #
or ##
.
Noncompliant code example
#define NonCompliant(a, b) # a ## b
int main() {
std::cout << NonCompliant(Hello, World);
}
The result of this code is unspecified. It will either print "HelloWorld" or trigger a compilation error. If ## is evaluated first this will print
HelloWorld. If # is evaluated first this will cause a compilation error telling that "Hello"World is not a valid preprocessor
token.
Compliant solution
#define Stringfy(a) #a
#define Compliant(a, b) Stringfy(a##b)
int main(){
std::cout << Compliant(Hello, World);
}
This example will always print "HelloWorld".