Define macro using _Generic gives typename not allowed

488 Views Asked by At

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
1

There are 1 best solutions below

0
Swift - Friday Pie On BEST ANSWER

_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); }