23

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zhinkaas
  • 295
  • 1
  • 3
  • 6
  • 1
    Well, after writing this question, I randomly did google and found below discussion http://stackoverflow.com/questions/1878285/iterate-over-struct-easily-display-struct-fields-and-values-in-a-richedit-box I think that answers all my doubts. Thanks everyone for answer though. – Zhinkaas May 03 '10 at 15:05

6 Answers6

30

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 :)

Kevin
  • 16,549
  • 8
  • 60
  • 74
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • I like the fusion.adapt.struct solution. [Asked specifically how to descend into nested struct fields and got it all working](http://stackoverflow.com/a/12102144/362754). – minghua Aug 24 '12 at 01:34
3

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.

Klaim
  • 67,274
  • 36
  • 133
  • 188
2

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:

struct simple {
    int a;
    char b;
    short d;
};

simple x {42, 'a', 3};
std::stringstream ss;

boost::pfr::for_each_field(
    x,
    [&ss](auto&& val) {
        ss << val << ' ';
    }
);

Answer migrated from a related/duplicate question - since somehow nobody mentioned this here.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
0

There isn't one, not in C++. Tough luck.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
  • There are various ways to have this: template metaprogramming, parsing or class generation with an external tool. Don't forget many things are possible with computers, though perhaps not easily or in the way you thought it should be. – Matthieu M. May 04 '10 at 06:27
  • [There are reflection libraries](https://github.com/rttrorg/rttr), and reflection is on its way into the standard. This answer is now out of date – Zoe Nov 18 '20 at 13:02
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cHao
  • 84,970
  • 20
  • 145
  • 172
0

Just do this

#include<iostream>
using namespace std;
struct abc  
{  
    int a;  
    char b;  
};
void display(abc myStruct){
   cout << "struct abc {"<< endl;
   cout << abc.a << endl;
   cout << abc.b << endl;
   cout << "}" << endl;
}
int main(){
    abc myStruct
    display(myStruct);
}

reflection is only required when the structs will be user defined and not by you and you are not sure about the internal structure of the struct.