有没有办法找出一个类是否是另一个类的直接基? [英] Is there a way to find out whether a class is a direct base of another class?
问题描述
我不知道是否有发现一个类是否是另一个类,即在升压型性状方面的直接基地 is_direct_base_of
函数的方法。据我所看到的,升压似乎并不支持这种功能,这使我觉得这是不可能与当前的C ++标准。
I'm wondering whether there is a way to find out whether a class is a direct base of another class i.e. in Boost type trait terms a is_direct_base_of
function. As far as I can see, Boost doesn't seem to support this kind of functionality, which leads me to think that it's impossible with the current C++ standard.
我希望它的原因是为了做一些验证在该用于一个反射系统,以指定一个类是从另一个导出两个宏检查,如在下面的例子中code
The reason I want it is to do some validation checking on two macros that are used for a reflection system to specify that one class is derived from another, as in the example code below.
header.h:
#define BASE A
#define DERIVED B
class A {};
class B : public A
{
#include <rtti.h>
};
rtti.h:
rtti.h:
// I want to check that the two macro's are correct with a compile time assert
Rtti<BASE, DERIVED> m_rtti;
虽然宏这个简单的例子似乎是不必要的,在我的现实世界中的场景 rtti.h
是一个复杂得多。
一个可能途径将是this指针的大小与该指针转换到基本类型的大小比较,不知怎么想弄清楚它是否是基类本身远或东西的尺寸。 (是的,你说得对,我不知道如何将工作,要么!)
One possible avenue would be to compare the size of the this pointer with the size of a this pointer cast to the base type and somehow trying to figure out whether it's the size of the base class itself away or something. (Yeah, you're right, I don't know how that would work either!)
推荐答案
我问自己,难道直接继承与间接区分什么C ++结构?它配备介意C ++派生类型的构造函数直接调用了他们唯一的直接基础(S)构造函数。所以,code是这样的:
I asked myself, "What C++ constructs do differentiate between direct inheritance vs. indirect?" It comes to mind that C++ constructors of derived types directly call constructors for their direct base(s) only. So code like this:
Derived::Derived() : Base() {}
只有当基本
有效期为的直接碱的
。而且,由于你是注射 rtti.h
的code进入人体导出
,你能容忍限制,这项技术仅仅是直接可见的派生类本身(即它并不像一般的假想 type_traits :: is_direct_base_of
,但不必是)。
Is only valid if Base
is is a direct base of Derived
. And since you are injecting the code of rtti.h
into the body of Derived
, you can tolerate the restriction that this technique is only directly visible within the derived class itself (i.e. it is not as general as a hypothetical type_traits::is_direct_base_of
, but does not need to be).
所以,因为我们可能不希望混乱与周围的默认构造本身,怎么样加入一些特殊用途的?
So since we probably don't want to mess around with the default constructors per se, how about adding some special-purpose ones?
#define BASE A
#define DERIVED B
struct rtti_tag {}; // empty type
class A
{
protected:
A(rtti_tag) { assert(false); } // never actually called
};
#include <rtti.h>
class B : public A
{
IS_DIRECT_BASE_OF(DERIVED, BASE); // fails to compile if not true
};
rtti.h
:
#define IS_DIRECT_BASE_OF(_B_, _A_) _B_(rtti_tag tag) : _A_(tag) \
{ assert(false); } // never actually called
这code编译我使用g ++ 4.2;如果我在继承层次插入一个新的类,断言休息和编译失败,我认为是一个合理的描述性诊断:
This code compiles for me with g++ 4.2; if I insert a new class in the inheritance hierarchy, the assertion breaks and compilation fails with what I think is a reasonably descriptive diagnostic:
In constructor ‘B::B(rtti_tag)’:
error: type ‘A’ is not a direct base of ‘B’
...
这篇关于有没有办法找出一个类是否是另一个类的直接基?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!