views:

138

answers:

7

Say 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 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?

+4  A: 

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

JSBangs
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.
+2  A: 

C++ compilers don't produce metadata. So there is no way to do such thing.

Andrey
C++ compilers don't produce metadata, but the metadata we are interested in is available at compile-time which calls for template metaprogramming :)
Matthieu M.
A: 

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.

cHao
A: 

This can't be done, because there is no introspection in C++. However, if you don't want to write the boilerplate code yourself, you can always write some tool to do it for you.

stefaanv
I think you have mistaken introspection for reflection. From the wikipedia article on introspection: http://en.wikipedia.org/wiki/Type_introspection, you can see an example of introspection in C++ using `typeid`.
Matthieu M.
Yep, I saw it too late. I left the answer for the boilerplate part.
stefaanv
A: 

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 -- site is down currently) that implement reflection features, so if you REALLY need reflection you should use one.

Klaim
+1  A: 

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

Matthieu M.
A: 

This can be done using the Visitor design pattern:

struct MyStruct;  // Forward declaration

struct Writer
{
    virtual void operator(MyStruct& m) = 0;
};

struct MyStruct
{
    void    write(Writer& w)
    {
        // Invoke the writer's method for this class.
        w(*this);
    }
    int a;
    double b;
};

struct File_Writer
{
    File_Writer(ofstream& output)
    : m_output(output)
    { ; }
    void operator(MyStruct& ms)
    {
        output << ms.a << endl;
        output << ms.b << endl;
    }
    ofstream&  m_output;
};

struct Display_Writer
{
    void operator(MyStruct& ms)
    {
        cout << "    a: " << ms.a << endl;
        cout << "    b: " << ms.b << endl;
    }
};

int main(void)
{
  MyStruct ms;
  ms.a = 5;
  ms.b = 4.2;

  // Write to the display, without changing methods in MyStruct
  Display_Writer  dw;
  ms.write(dw);

  // Write to a file without changing methods in MyStruct
  ofstream  my_file("me.txt");
  File_Writer    fw(my_file);
  ms.write(fw); // Notice that only the writer object changed.

  return 0;
}
Thomas Matthews
Visitor still means that you have to write the boilerplate code yourself, which the OP specifically wish NOT to do.
Matthieu M.