微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++中的成员反射列表

如何解决C++中的成员反射列表

在 C++ 中,无法获取有关类成员变量的信息。这使得自动序列化等功能难以实现。似乎将此功能添加到语言中很简单。毕竟 typeidstd::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++ 标准中添加与此类似的功能?如果已经考虑并拒绝了这样的功能,为什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。