【发布时间】:2022-01-21 08:02:14
【问题描述】:
我想在继承重复时引发错误。这是我找到它的方法。
#include <utility>
class Person {};
class Man : public Person {};
class Woman : public Person {};
template <typename... Types>
class merge_class : public Types... {};
template <typename... Types>
struct condition
{
using merge = merge_class<Types...>;
using type = std::enable_if<
std::is_convertible<merge, Person>::value // condition
, merge>::type;
};
class BummooKim : public condition<Man>::type {};
class Daniel : public condition<Woman>::type {};
//class Unkown : public condition<Man, Woman>::type {}; // There is an error in the declaration.
但是,我发现如果有非默认构造函数,这种方式就不能用了。
我想知道是否有关键字表明它必须是单继承的。
如果 c++ 不支持“关键字”,我想要另一种方式。
示例
class OtherWay : public condition<Man, Other>::type
{
OtherWay() : Man() {}
};
【问题讨论】:
-
不确定,但这似乎类似于继承中的钻石问题 - 人 -> 男人,女人 -> 未知。
-
我发现自己遇到的问题是混杂的,不清楚的。
-
我一头雾水,要不要解决钻石继承的这个错误?如果是,请阅读this。
-
我想抛出错误而不是使用虚拟公共。
-
我认为这种做法类似于在虚拟继承中创建一个虚拟类。我通过向虚拟类添加条件来实现它。
标签: c++ templates inheritance