TMP:如何编写模板代码,将任何结构转换为元组? [英] TMP: how to write template code which converts any struct into a tuple?

查看:157
本文介绍了TMP:如何编写模板代码,将任何结构转换为元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用模板元编程将任何结构或类转换为元组?



例如:

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

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

这将是很好的有一些模板代码将自动为我生成Foo_Tuple。



ANSWER



这是一个简单的例子, ORM或任何时候你需要写很多的板式代码,而一个只是模板或宏是不足的任务), Boost Mirror 看起来可能非常有用。我已经挖掘Boost镜像一点:基本的反射功能(在镜像和Puddle)不难理解,是很容易设置,似乎是相当广泛(可以处理许多构造,包括C ++ 11枚举类等)。我发现这个基本功能是足够的 - 我可以只使用MACROS到我想要暴露我的类到反射(所以我不必编写板文件代码)。工厂生成器似乎非常强大(与相同的初始宏设置,你可以交换任何工厂生成器你喜欢输出JSON,SOCI或流等),但有一个更大的学习曲线/设置,如果你想写自己的工厂发电机。最后几个注释:有一些小的调整,我能够使它与C ++ 11在gcc 4.7.2上工作;

解决方案

我不知道该如何解决问题,我认为有一种方法可以在C ++中做到这一点。



我不知道一种枚举结构中的字段/类型的方法 - 如果你能这样做,我认为构造这样一个元组将是相当简单。



我相信Boost.Fusion有一个宏,帮助这个名为FUSION_ADAPT_STRUCT,但这是所有手动。 / p>

这个技术术语是反射,你可以通过搜索C ++反射找到很多关于它的信息。



以下是一篇文章:如何向C ++应用程序添加反射?


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.

解决方案

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?

这篇关于TMP:如何编写模板代码,将任何结构转换为元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆