Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

windows

dump

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

like image 934
Alan Avatar asked Oct 14 '22 23:10

Alan


2 Answers

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++);
  }
}
like image 150
sbi Avatar answered Oct 19 '22 03:10

sbi


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.

like image 44
Tony Delroy Avatar answered Oct 19 '22 02:10

Tony Delroy