Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

visual c++ - Printing values of all fields in a C++ structure

Consider a simple structure:

struct abc  
{  
    int a;  
    char b;  
}  

I got some value in a variable defined as its structure, and now I want to print the below.

*a = [some value]  
b = [some character]*

What is the best way to achieve this for an arbitrary structure without having to write a dump...(...) function for each of the structure I encounter?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems you already found the solution, but I'll expand a bit.

What you are calling for is called Reflection, i.e. the ability for an object to describe itself.

Most languages can implement reflection thanks to metadata. In Python for example, the functions and attributes of an object are stored in a dictionary element.

C++ does not have any native reflection system unlike C# or Java, which prevents (for example) this kind of automatic printing / serialization or deserialization.

However, C++ has very powerful metaprogramming support which allows us (through the use of templates) to emulate reflection (at compile-time). This is usually done using Boost.Fusion, a library meant for crossing over from compile-time to run-time.

As the example demonstrated in your link, the BOOST_FUSION_ADAPT_STRUCT macro allows you to take a standard struct and give it the required interface to be treated as a Fusion.Sequence.

Another example would be to use Fusion.Vector or Fusion.Map to store the attributes of the class and then expose this Sequence to automatic print/serialization/deserialization methods.

There is a limitation to this system however: Metaprogramming does not mesh well with OO-programming.

struct Base { char a; };            // Adapt
struct Derived: Base { char b; };   // Adapt

void print(Base const& b) { boost::fusion::for_each<Base>(b, Print()); }

will only print the member of Base (here a). When using polymorphism, you need to use virtual methods at one point or another :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...