如何在编译时查询类的所有基类?

编程入门 行业动态 更新时间:2024-10-28 01:12:43
本文介绍了如何在编译时查询类的所有基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用 std :: is_base_of< A,B> :: value ,可以检查类 A 基类 class B 。 也可以查询编译器的类 B 的所有基类,例如, base_classes_of< B> 返回包含 B 的所有基类的std :: tuple?

With std::is_base_of<A,B>::value one can check if a class A is a base class of class B. Is it also possible to query the compiler for all base classes of a class B, e.g., something like base_classes_of<B> returning a std::tuple containing all base classes of B ?

有evtl。 g ++中的非标准扩展可以实现这一点?

Is there evtl. a non-standard extension in g++ that can accomplish this ?

如果这是不可能的,有没有人知道为什么?这听起来像一个相当根本的的信息,编译器很容易应该可用。

If this is not possible at all, does anyone know why? It sounds like a rather fundamental piece of information the compiler easily should have available?

示例:

#include <type_traits> #include <tuple> struct A {}; struct B : A {}; static_assert(std::is_base_of<A, B>::value, "A is base of B"); static_assert(! std::is_base_of<B, A>::value, "but B is not base of A"); // now I am looking for something like // typedef base_classes_of<B>::type B_bases; // static_assert(std::is_same<B_bases, std::tuple<A>>::value, "all bases of B are: A"); int main() {}

推荐答案

在 基础和 direct_bases www.open-std/jtc1/sc22/wg21/docs/papers/2009/n2965.htmlrel =nofollow> N2965 。 对于数据成员,由于数据成员可以是位字段,它们的类型特征有一些细微之处。 另一方面,基类没有这样的问题。 我认为在某些情况下对某些编译时查询的需求如 bases ,如N2965的激励示例中所述。 然而,不幸的是,目前的C ++只是缺乏它,就我所见, GCC和Clang-C ++似乎暂时不提供类似的设施...

Similar facility bases and direct_bases were proposed in N2965. As for data members, since data members can be bit-fields, their type traits have some subtleties. On the other hand, base classes don't have such problems. I think there is the demand on some compile-time query like bases in some situations, as mentioned in Motivating examples of N2965. However, unfortunately, current C++ just lacks it, and as far as I saw, GCC and Clang-C++ seem not to provide similar facilities at the moment...

更多推荐

如何在编译时查询类的所有基类?

本文发布于:2023-11-15 22:04:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598362.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!