generic way to print out variable name in c++

前端 未结 6 771
难免孤独
难免孤独 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条回答
  •  萌比男神i
    2020-12-05 21:43

    It's not possible (see the other answers).

    One workaround for this is to use automatic code generation. You write the field definitions in a file and then generate the .h and .cpp files from it. I used this for a code which had around 100 classes with lots of fields. It was able to generate the code for sending them to streams (mostly debugging) and for socket communication. It's very reliable (never had to test any of those functionalities), but since it's not pure C++ it might not be the solution for you.

提交回复
热议问题