English 中文(简体)
我如何利用静态质和型号防止在nes式模版中的钻石模式? [复制]
原标题:How do I prevent diamond pattern in nested template types using static assert and type traits? [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Is there a way to prevent a class from being derived from twice using a static assert and type trait?

我想要防止的是,在D条中产生的基于C的模板不止一个(即从中只应当有一个C的例子)。 希望在C或B条中作出可能解决的静态主张。

// My Classes
template <class T>
class A {};

class B {};

template <class T, class S>
class C : public B, public virtual A<T> {};

// Someone elses code using my classes
class D : public C<Type1, Type2>, public C<Type3, Type4>
{
};
最佳回答

目前,不可能在<代码>B或C上发现另一类产品从中继承,因此你可以在此添加一个说法。 然而,通过增加“紧急回收”模板参数,你可以说明“编码”C的衍生类别是什么。 不幸的是,这确实要求衍生产品类别提出正确的模板论点,而且没有办法加以执行。

然后,你可以确定衍生产品类别是否以不止一种方式继承<代码>B;is/em> 基本类别,但can t将衍生产品位转换为 B*(因为转换模棱两可)。 请注意,这并不一定表明多重继承;如果没有公有继承,检验也会失败。

因此,我认为最佳解决办法是:

#include <type_traits>

template <class T> class A {};
class B {};

template <class T, class S, class D>
class C : public B, public virtual A<T> {
public:
    C() {
        static_assert(
            std::is_base_of<C,D>::value && std::is_convertible<D*,B*>::value, 
            "Multiple inheritance of C");
    }
};

struct Type1 {};
struct Type2 {};
struct Type3 {};
struct Type4 {};

class Good : public C<Type1, Type2, Good> {};
class Evil : public C<Type1, Type2, Evil>, public C<Type3, Type4, Evil> {};

int main()
{
    Good good;
    Evil evil; // Causes assertion failure
}

我不得不在建筑商而不是班级定义中提出这种说法,因为有些类型在上调时不完整。 不幸的是,这意味着,只有实际上报的班级才会报告错误。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?