windows c/c++ 中是否有类似 PHP 的 var_dump 之类的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3861769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there something like var_dump of PHP in c/c++?
提问by Alan
I'm seeking of an API that can be used to dump most data structures,is there one in c/c++?
我正在寻找一种可用于转储大多数数据结构的 API,在 c/c++ 中有一个吗?
回答by sbi
I'm seeking of an API that can be used to dump most data structures,is there one in c/c++?
我正在寻找一种可用于转储大多数数据结构的 API,在 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.
更长的答案:C++ 没有反射。也就是说,没有办法在运行时分析未知的数据结构。您必须自己为要转储的任何数据结构编写转储例程,构建在其数据成员可用的基础上。
However, note that C++ has a whole lot of tools to make that easier. For example, given a simple generic dump()
template:
但是,请注意,C++ 有很多工具可以使这变得更容易。例如,给定一个简单的通用dump()
模板:
template< typename T >
inline void dump(std::ostream& os, const T& obj) {os << obj;}
the elements of any sequencecan 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++);
}
}
回答by Tony Delroy
boost has a serialisation library you can explicitly use to make your data structures dumpable.
boost 有一个序列化库,您可以明确使用它来使您的数据结构可转储。
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.
如果您希望它更自动地发生,那么您的选择就很黯淡。C++ 程序可以检查它自己的调试符号,或者编译一些额外的代码——可能是参考 GCC-XML 输出自动生成的,或者使用像 OpenC++ 这样的工具来自动生成一些元数据。