c++visual-c++typenamevisual-studio-macros

Define macro using _Generic gives typename not allowed


I am trying to define a generic macro, which I intend to use with exception handling while debugging the code. When I try to compile the code below, it says typename not allowed. I am a noob when it comes to the macros, any and all help is highly appreciated.


#define ASSERTEXCP(x) _Generic((x),\
char *: printf( "assertion error line %d, file(%s):-> %s\n", __LINE__, __FILE__, x );
char strMsg[2014] = {'\0'}; \
sprintf(strMsg, "\nassertion error line %d, file(%s):-> %s\n", __LINE__, __FILE__, x); \
OutputDebugString(strMsg););


#endif

Solution

  • _Generic is the C approach to C++ overloading. C++ approach is to use if constexpr or an overloaded function:

        #include <type_traits>
    
        #define ASSERTEXCP(x) if constexpr (std::is_same<decltype(x), char *>::value) {  \
        printf( "assertion error line %d, file(%s):-> %s\n", __LINE__, __FILE__, x ); \
        char strMsg[2014] = {'\0'}; \
        sprintf(strMsg, "\nassertion error line %d, file(%s):-> %s\n", __LINE__, __FILE__, x); \
        OutputDebugString(strMsg); }