c++visual-c++preprocessorc-preprocessorcompiler-bug

C++ Preprocessor Standard Behaviour


I'm studying the C++ standard on the exact behaviour of the preprocessor (I need to implement some sort of C++ preprocessor). From what I understand, the example I made up (to aid my understanding) below should be valid:

#define dds(x) f(x,
#define f(a,b) a+b
dds(eoe)
su)

I expect the first function-like macro invocation dds(eoe) to be replaced by f(eoe, (note the comma within the replacement string) which then considered as f(eoe,su) when the input is rescanned.

But a test with Visual C++ 2010 gave me this (I told the VC++ to output the preprocessed file):

eoe+et_leoe+et_l
su)

This is counterintuitive and is obviously incorrect. Is it a bug with Visual C++ 2010 or my misunderstanding of the C++ standard? In particular, is it incorrect to put a comma at the end of the replacement string like I did? My understanding of the C++ standard grammar is that any preprocessing-token's are allowed there.

I don't have GCC or other versions of Visual C++. How can I verify with these compilers?


Solution

  • To the best of my understanding there is nothing in the [cpp.subst/rescan] portions of the standard that makes what you do illegal, and clang and gcc are right in expanding it as eoe+su, and the MSC (Visual C++) behaviour has to be reported as a bug.

    I failed to make it work but I managed to find an ugly MSC workaround for you, using variadics - you may find it helpful, or you may not, but in any event it is:

    #define f(a,b) (a+b
    #define dds(...) f(__VA_ARGS__)
    

    It is expanded as:

    (eoe+
    su)
    

    Of course, this won't work with gcc and clang.