首页 > 解决方案 > C++中的成员反射列表

问题描述

在 C++ 中,无法获取有关类的成员变量的信息。这使得自动序列化等功能难以实现。似乎此功能很容易添加到语言中。毕竟typeid,并且std::typeinfo可以获得类型的唯一标识符。为了说明这样的功能如何工作,请参见下文:

// An integer constant corresponding to the number of members in the struct
type_members(A)::count

// An array of type_info corresponding to the members in the struct
type_members(A)::typeids

// An array of null terminated char* corresponding to the names of the members
type_members(A)::names

// Ann array of T A::* corresponding to the memory offsets of the members
// Where T is the type of the member at that index
type_members(A)::members

是否讨论过在 C++ 标准中添加与此类似的功能?如果这样的功能已被考虑并拒绝,为什么?

标签: c++rtti

解决方案


推荐阅读