7

Is it possible to use template meta-programming to convert any struct or class into a tuple?

For instance:

struct Foo
{
  char         c;
  int          i;
  std::string  s;
};

typedef std::tuple< char, int, std::string >  Foo_Tuple;

It would be nice to have some template code which will generate Foo_Tuple automagically for me.

ANSWER

This is overkill for such a simple case, but for more elaborate cases (eg ORM or any time you need to write a lot of boiler-plate code, and a mere template or macro is inadequate for the task), Boost Mirror looks like it may be extremely useful. I have dug into Boost Mirror a bit more: the basic reflection functionality (in Mirror and Puddle) are not hard to understand, are quite easy to set-up and seem to be quite extensive (can handle many constructs, including C++11 enum classes, etc...). I find this basic functionality to be more than adequate - I can just use the MACROS to the extent that I want to expose my classes to Reflection (so that I don't have to write boiler-plate code). The Factory generators also seem to be very powerful (with the same initial macros set up, you can swap in any factory generator you like to output JSON, SOCI, or to a stream etc...), but has a larger learning curve/setup, if you want to write your own factory generators. One last couple of notes: with some minor tweaks, I was able to get it to work with C++11 on gcc 4.7.2; also, the documentation has been well DOxygenated and there seem to be more than sufficient examples to get going quickly.

kfmfe04
  • 14,936
  • 14
  • 74
  • 140
  • I know I'll be loudly condemned for suggesting it, but I think in this case `reinterpret_cast` will work on most platforms. – James Brock Dec 15 '12 at 06:25
  • 2
    @JamesBrock Boo! Hiss! [Your suggestion's bad and you should feel bad](http://www.youtube.com/watch?v=jG2KMkQLZmI)! – John Kugelman Dec 15 '12 at 06:35
  • I don't think that `reinterpret_cast` will work (in general), because different implementations of `tuple` lay out the members differently. In particular, one implementation might lay it out `char, int, string`, while another might use `string, int, char`. – Marshall Clow Dec 15 '12 at 06:38
  • 2
    @John Kugelman I do feel bad, I do. – James Brock Dec 15 '12 at 06:52
  • 3
    @jamesBrock, it won't work with the Gnu libstdc++ on any platform, because the Gnu tuple is laid out in reverse order. The clang libc++ has tuple elements in standard order, but it's not in common use. I have no idea what other std lib implementations do; presumably most of them do one or the other. – rici Dec 15 '12 at 06:55
  • @rici +1 for useful implementation details – kfmfe04 Dec 15 '12 at 07:02
  • You may find this proposal interesting: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3326.pdf – mythagel Dec 17 '12 at 01:31

1 Answers1

7

I don't think that there's a way to do this in C++.

I don't know a way to enumerate the fields/types in a struct - if you could do that, I would think that constructing such a tuple would be fairly straightforward.

I believe that Boost.Fusion has a macro that helps with this called FUSION_ADAPT_STRUCT, but that's all manual.

The technical term for this is "reflection", and you can find lots of information about it by searching for "C++ reflection".

Here's one such article: How can I add reflection to a C++ application?

Community
  • 1
  • 1
Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
  • looks like until C++ gets reflection, some of these libraries will do (at least it will prevent tons of boilerplate code). – kfmfe04 Dec 17 '12 at 03:58