Search code examples
c++c-preprocessormetaprogramming

Is there any C++ code that can't be passed through an identity preprocessor macro?


Given:

#define IDENTITY(...) __VA_ARGS__

Is there any code that the compiler will reject, that it would normally accept, when wrapped in an invocation of this macro? The simpler #define IDENTITY(x) x will fail if there are any commas but the above seems pretty robust, GCC happily accepts this:

#include <stdio.h>

#define IDENTITY(...) __VA_ARGS__

IDENTITY(
    struct does_this_work {
        float yes;
        double appears_to;
    };

    template<class T> int what_about_this() {
        int x = 50, y = 0; // see the comma!
        for(int i = 0; i < x; ++i) {
            printf("Hello world!");
        }
        return 3;
    }
)

Solution

  • #define IDENTITY(...) __VA_ARGS__
    #define CAT(X,Y) X##Y
    
    IDENTITY(CAT) IDENTITY((1,2))  // => CAT (1,2)
    
    CAT (1,2) // => 12