从C ++中的POD结构中获取可用数据成员的列表 [英] Get list of available data member from a POD struct in C++

查看:117
本文介绍了从C ++中的POD结构中获取可用数据成员的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题听起来有点不寻常。让我们使用一个POD结构:

the question can sound a bit unusual. Let's take a POD struct:

struct MyStruct
{
   int myInt;
   double myDouble;
   AnotherPOD* myPointer;
};

编译器知道可用数据成员的列表。你知道在编译时(更好)或在运行时获取数据成员名称(和类型)列表的任何方式吗?

The compiler knows the list of available data members. Do you know any way to get list of data member name (and type) either at compile time (better) or at run time?

我有一个庞大的POD我想自动化创建操作符< <。

I have a huge amount of POD structs and I would like to automate the creation of operator<<.

我知道我可以为头文件创建一个解析器,创建一些文件并编译它们。

I know I could create a parser for the header files, create some files and compile those. However, I am sure the compiler has already had this information and I would like to exploit it.

有没有想法?

感谢

推荐答案

BOOST_FUSION_ADAPT_STRUCT 引入了编译时反映(这是真棒)。

BOOST_FUSION_ADAPT_STRUCT introduces compile-time reflection (which is awesome).

你可以把它映射到运行时反射当然,它不会太容易,但它是可能在这个方向,而不会是相反的:)

It is up to you to map this to run-time reflection of course, and it won't be too easy, but it is possible in this direction, while it would not be in the reverse :)

这篇关于从C ++中的POD结构中获取可用数据成员的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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