How do you create a debug only function that takes a variable argument list? Like printf()

后端 未结 14 2181
心在旅途
心在旅途 2020-12-23 09:44

I\'d like to make a debug logging function with the same parameters as printf. But one that can be removed by the pre-processor during optimized builds.

<
14条回答
  •  半阙折子戏
    2020-12-23 10:16

    Not exactly what's asked in the question . But this code will be helpful for debugging purposes , it will print each variable's value along with it's name . This is completely type independent and supports variable number of arguments. And can even display values of STL's nicely , given that you overload output operator for them

    #define show(args...) describe(#args,args);
    template
    void describe(string var_name,T value)
    {
        clog<
    void describe(string var_names,T value,Args... args)
    {
        string::size_type pos = var_names.find(',');
        string name = var_names.substr(0,pos);
        var_names = var_names.substr(pos+1);
        clog<

    Sample Use :

    int main()
    {
        string a;
        int b;
        double c;
        a="string here";
        b = 7;
        c= 3.14;
        show(a,b,c);
    }
    

    Output :

    a = string here | b = 7 | c = 3.14 
    

提交回复
热议问题