Type overloading macro

后端 未结 3 1144
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-15 23:04

I have a bunch of printf debug helper macros and it would be pretty cool to have to not specify the type, is there anything you can do to allow something like macro overloading

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-15 23:37

    I think you can give a try with following code:

    #define DPRINT(fmt) do{ \
                my_printf fmt ; \
            } while(0)
    
    my_printf( const char* szFormat, ... )
    {
        char szDbgText[100];
    
        va_start(args, szFormat);
        vsnprintf(&szDbgText,99,szFormat,args);
        va_end(args);
    
        fprintf( stderr, "%s", szDbgText );
    }
    // usage
    func( )
    {
        int a = 5;
        char *ptr = "hello";
    
        DPRINT( ("a = %d, ptr = %s\n", a, ptr) );
    }
    

    Note: DPRINT usage takes dual parentheses here.

提交回复
热议问题