TMP: how to write template code which converts any struct into a tuple?

前端 未结 1 444
滥情空心
滥情空心 2020-12-20 18:40

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

For instance:

struct Foo
{
  char         c;
  int               


        
相关标签:
1条回答
  • 2020-12-20 19:08

    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?

    0 讨论(0)
提交回复
热议问题