在编译期间检测继承

时间:2010-12-25 02:31:18

标签: c++

我无法弄清楚为什么这段代码返回false。我有部分专业化的第一个版本。它没用,我尝试了第二个版本。它也没用。

更新:我想检查“Derived”是否是从“Base”公开派生的。

更新:

    template<typename TDerived, typename TBase>
    struct Derived_From
    {

    public:
        static void constraints(TBase*, TDerived* ptr) { TBase* b = ptr; ignore(b); }
        Derived_From() { void (*p)(TBase*, TDerived*) = constraints; ignore(p);}
    };

我在Strostrup的主页上找到了上面的代码片段。但是,如果派生类不是从Base公开派生的,它就不会让代码编译。

template<class TBase, class TDerived>
struct IsDerived
{
    public:
    enum { isDerived = false };
};


template<class TBase>
struct IsDerived<TBase, TBase>
{
    public:
    enum {  isDerived = true };
};


template<class TBase>
struct IsDerived<TBase&, TBase&>
{
    public:
    enum {  isDerived = true };
};

int main()
{
    cout << ((IsDerived<Base&, Derived&>::isDerived) ? "true" : "false")
         << endl;
    cout << ((IsDerived<const Derived*, const Base*>::isDerived) ?
            "true" : "false") << endl;

} 

5 个答案:

答案 0 :(得分:7)

结帐boost type traits,特别是is_base_of模板。

答案 1 :(得分:7)

我总是只使用指针初始化。指针隐式转换为超类型(可能是身份转换或公共基类),因此除非存在该关系(并且方向正确),否则它将无法编译。

e.g。

Parent* p = (Possibly_Derived*)0;
等等,你不想让编译失败,而是设置一个变量?这里:

template<typename TParent>
bool is_derived_from( TParent* ) { return true; }

template<typename TParent>
bool is_derived_from( void* ) { return false; }

cout << is_derived_from<Parent>( (Possibly_Derived*)0 );

以下是演示:http://ideone.com/0ShRF

答案 2 :(得分:1)

首先,我假设您希望一个泛型方法适用于任何类,而无需修改或向该类添加任何函数。我认为这种模板方法无法正常工作。派生类与基类的相等,除非这两个类是EQUAL,否则不会实例化专用结构。类似地,指向派生类的指针与相等指向基类的指针。

我假设您已经知道人们经常使用虚函数来检查“对象”是基类还是派生类。

(请注意,我说你想做什么是不可能的 - 只是你所看到的结果是预期的,模板方法是行不通的。)

答案 3 :(得分:0)

检查这是否有帮助::

Template type check C++

答案 4 :(得分:0)

如果我是对的,你用两种不同的参数类型调用模板。

(IsDerived<Base&, Derived&>::isDerived)

因此它会调用

struct IsDerived
{
    public:
    enum { isDerived = false };
};

这就是IsDerived<Base&, Derived&>::isDerived以及IsDerived<const Derived*, const Base*>::isDerived为假的原因。

(IsDerived<Base&, Base&>::isDerived)之类的调用将返回True。

  

我想检查“Derived”是否是   公开源自“基地”。

我不知道基于模板的解决方案,并将密切关注此线程。但是如果需要的话,我通常会利用dynamic_cast来完成这项工作。

如果dynamic_cast无法转换指针,因为它不是所需类的完整对象,则返回空指针以指示失败。

如果使用dynamic_cast转换为引用类型并且无法进行转换,则会抛出类型为bad_cast的异常。