C preprocessor problem in Microsoft Visual Studio 2010

Posted by Remo.D on Stack Overflow See other posts from Stack Overflow or by Remo.D
Published on 2010-05-17T17:03:50Z Indexed on 2010/05/18 4:41 UTC
Read the original article Hit count: 191

Filed under:
|

I've encountered a problem with the new Visual C++ in VS 2010.

I've got a header with the following defines:

#define STC(y) #y
#define STR(y) STC(\y) 
#define NUM(y) 0##y

The intent is that you can have some constant around like

#define TOKEN x5A

and then you can have the token as a number or as a string:

NUM(TOKEN) -> 0x5A
STR(TOKEN) -> "\x5A"

This is the expected behavior under the the substitution rules of macros arguments and so far it has worked well with gcc, open watcom, pellesC (lcc), Digital Mars C and Visual C++ in VS2008 Express.

Today I recompiled the library with VS2010 Express only to discover that it doesn't work anymore! Using the new version I would get:

NUM(TOKEN) -> 0x5A
STR(TOKEN) -> "\y"

It seems that the new preprocessor treats \y as an escape sequence even within a macro body which is a non-sense as escape sequences only have a meaning in literal strings.

I suspect this is a gray area of the ANSI standard but even if the original behavior was mandated by the standard, MS VC++ is not exactly famous to be 100% ANSI C compliant so I guess I'll have to live with the new behavior of the MS compiler.

Given that, does anybody have a suggestion on how to re-implement the original macros behavior with VS2010?

© Stack Overflow or respective owner

Related posts about c

    Related posts about msvc