generic way to print out variable name in c++

前端 未结 6 758
难免孤独
难免孤独 2020-12-05 20:47

given a class

struct {
  int a1;
  bool a2;
  ...
  char* a500;
  ...
  char a10000;      
}

I want to print or stream out



        
6条回答
  •  醉梦人生
    2020-12-05 21:28

    The watch macro is one of the most useful tricks ever.

    #define watch(x) cout << (#x) << " is " << (x) << endl
    

    If you’re debugging your code, watch(variable); will print the name of the variable and its value. (It’s possible because it's built during preprocessing time.)

提交回复
热议问题