用于可变参数模板的基类列表
有没有办法访问 C++ 中某个类的所有基类? 由于可变参数模板可用,我认为使用例如 ___BASE 是有意义的,它是当前类派生自的所有类型的可变参数列表。 这还可以检查类或其基类是否属于某种类型。 这已经可能了吗,还是我在这里遗漏了一些东西,为什么这是不可能的!?
Is there a way to access all base classes of a class in C++?
Since variadic templates are available I think it would make sense to have e.g. ___BASE which is a variadic list of all types the current class does derive from.
This would also enable a check whether a class or it's base classes are of a certain type.
Is this already possible or am I missing something here why this isn't possible!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管您可以自由地向类层次结构的所有成员添加合适的 typedef 以使此信息可用(例如 typedef 一个元组,其参数类型是祖先加上祖先的祖先)——你可能可以为此编写一个小混合。
至于检查某个东西是否是另一个东西的基础,这已经是 std::is_base_of 类型特征形式的标准的一部分。
There isn't a general mechanism in the language that'll tell you all the base classes, though you're free to add a suitable typedef to all members of your class hierarchy that will make this information available (e.g. typedef a tuple whose argument types are the ancestor plus the ancestor's ancestors) -- you could probably write a little mixin for that.
As for checking whether something is a base of another, that's already part of the standard in the shape of the
std::is_base_of
type trait.