开发者

Is there something like var_dump of PHP in c/c++? [duplicate]

This question already has answers here: Dump facility in C++ like var_dump() in PHP? (7 answers) Closed 6 years ago.

I'm seeking of an API that can be used开发者_如何学运维 to dump most data structures,is there one in c/c++?


I'm seeking of an API that can be used to dump most data structures,is there one in c/c++?

Short answer: No, there is not.

Longer answer: C++ doesn't have reflection. That is, there is no way to analyze unknown data structures at runtime. You will have to write dump routines yourself for any data structure you want to dump, building on what's available for its data members.

However, note that C++ has a whole lot of tools to make that easier. For example, given a simple generic dump() template:

template< typename T >
inline void dump(std::ostream& os, const T& obj) {os << obj;}

the elements of any sequence can be dumped using this simple function:

template< typename OutIt >
void dump(std::ostream& os, OutIt begin, OutIt end)
{
  if(begin != end)
    os << *begin++;
  while(begin != end) {
    os << ", ";
    dump(*begin++);
  }
}


boost has a serialisation library you can explicitly use to make your data structures dumpable.

If you want it to happen more automatically, your options are bleak. A C++ program can inspect its own debug symbols, or compile up some extra code - perhaps auto-generated with reference to GCC-XML output, or using a tool like OpenC++ to auto-generate some meta-data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜