Home >Backend Development >C++ >Why Does Stringification Produce 'foo' in One Case and '4' in Another?
Stringification Unraveled: A Step-by-Step Explanation
Stringification is a powerful macro technique that converts a given token into a string literal. To understand its intricacies, let's examine the following scenario:
With str(foo), the expected output is "foo" because stringification is believed to occur before text expansion. However, in the second example:
The output of xstr(foo) becomes "4." To unravel this discrepancy, we delve into the specific steps involved in macro expansion:
Applying these steps to xstr(foo):
Now, let's examine str(foo):
The crux of the issue lies in the sequence of execution. In step 1, when str(foo) is evaluated, foo has not yet been replaced with 4. Therefore, the stringification step results in "foo."
Hence, the use of a helper macro like xstr allows for separate execution of steps 2 and 1, ultimately providing the desired stringification behavior.
The above is the detailed content of Why Does Stringification Produce 'foo' in One Case and '4' in Another?. For more information, please follow other related articles on the PHP Chinese website!