问题描述
使用 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 ofclass B
. Is it also possible to query the compiler for all base classes of a classB
, 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 fundamentalpiece 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() {}
推荐答案
在
。
对于数据成员,由于数据成员可以是位字段,它们的类型特征
有一些细微之处。
另一方面,基类没有这样的问题。
我认为在某些情况下对某些编译时查询的需求如 bases
,如N2965的激励示例中所述。
然而,不幸的是,目前的C ++只是缺乏它,就我所见,
GCC和Clang-C ++似乎暂时不提供类似的设施...
Similar facility bases
and direct_bases
were proposed inN2965.
As for data members, since data members can be bit-fields, their type traitshave 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...
这篇关于如何在编译时查询类的所有基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!