Unexpected behavior of the GCC and VC ++ preprocessor

I am trying to understand the requirement of a C ++ standard preprocessor. The slightly complex example that I created causes unexpected results in GCC and VC ++ 2010:

#define a(x,y) x##y #define tzsW kka a(t,zs )W 

GCC gives:

 tzs W 

Note the extra space added before W.

VC ++ 2010 gives:

 tzsW 

Note that no space is added to W, but the identifier does not expand. I looked at the C ++ 03 standard and cannot find anything saying that we must prevent the creation of a new identifier ( tzsW ), as in gcc. And it costs nothing to prevent this new identifier from further expanding macros (VC ++ behavior).

Why don't GCC and VC ++ 2010 like the new identifier?

EDIT

if another macro use is used, for example

 a(t,zs )[] 

gcc gives:

 tzs[] 

Notice that a space has been added, showing that gcc intentionally adds space to my previous case.

+1
c ++ gcc c-preprocessor visual-c ++
source share
2 answers

The preprocessor output is tokens, not plain text. If you do not use the token labeling operator, tokens are usually not combined during pre-processing.

When you look at the output of the preprocessing step, you need to convert the tokens into text. gcc inserts space, so you are not fooled into thinking that tzsW is the only token. This does not need to be done in the case of tzs[ , because [ not a valid identifier character, so there is no confusion.

Both compilers are correct without considering tzsW as one token to be redeployed.

Please note that the Visual C ++ Documentation confirms that compiling the preprocessor output may lead to different and incorrect comparison results with compiling the original source due to the fact that they do not insert spaces for delimiting tokens when converting pre-processed output to text. This does not happen when the preprocessor output is passed directly to the next stage of the compiler during normal operation.

+4
source share
  • Space or space?

    I did not find the link in the standard, but this article discusses this problem. In short, it is not easy to decide whether you want this space or not ...

  • Replacing tzsW with kka or not?

    See 16.3.4 in the standard:

    After all parameters in the substitution list have been replaced, the resulting sequence of preprocessing tokens is rescanned with all subsequent preprocessing tokens of the source file for more macro names to replace.

    This indicates that VC behavior is incorrect here.

+1
source share

All Articles