Type overloading macro

后端 未结 3 1142
佛祖请我去吃肉
佛祖请我去吃肉 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:49

    How about the following macro? It still requires you to pick the print format but you won't have to redefine all possible cases and it works on MSVC as well:

    #define DPRINT(t,v) printf("The variable '%s' is equal to '%" ## #t ## "' on line %d.\n",#v,v, __LINE__)
    

    To use it:

    int var = 5;
    const char *str = "test";
    DPRINT(i,var);
    DPRINT(s,str);
    

提交回复
热议问题