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?
It seems you already found the solution, but I'll expand a bit.
What you are calling for is called
Reflection
, ie the ability for an object to describes itself.Most languages 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, this library being 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 standardstruct
and give it the required interface to be treated as a Fusion.Sequence.Another example would be to use
Fusion.Vector
orFusion.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.
will only print the member of
Base
(herea
). When using polymorphism, you need to usevirtual
methods at one point or another :)There isn't one, not in C++. Tough luck.
If you're using C++ in .NET, you can potentially use the System.Reflection stuff to look at the innards of your structure. Unmanaged C++ rarely, if ever, stores that kind of metadata about objects, though.
With C++17 (perhaps even C++14), and some crazy Russian hacks - it can be partially done. That is, you can print the types values in sequence, but you can't get the field names.
The relevant library is Antony Polukhin's "magic_get". Specifically, it offers a "for each field" iteration mechanism, which takes a templated lambda with
auto
parameter type. Example:Answer migrated from a related/duplicate question - since somehow nobody mentioned this here.
You need "reflection" to do this. Reflection is not provided natively in C++, or only for minimal informations (type ids/names).
There are libraries (like CAMP) that implement reflection features, so if you REALLY need reflection you should use one.