Bli*_*ard 3 c++ inheritance templates
所以,我有一个钻石层次结构。
class Base {
// ...
}
class Derived_A : public Base {
// ...
}
class Derived_B : public Base {
// ...
}
class Join : public Derived_A, public Derived_B {
// ...
}
Run Code Online (Sandbox Code Playgroud)
根据模板变量,我想有条件地选择继承 A 和/或 B。(我理解菱形结构,A 和 B 可以虚拟继承。)我所拥有的是:
template<bool HAS_A, bool HAS_B>
class Join : public Derived_A, // enable if HAS_A
public Derived_B // enable if HAS_B
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用std::enable_if_t
,但我不确定当它的布尔值是false
.
指定基类而不是bool
用作模板参数似乎更简单。例如
template<class... Base> class Join : public Base... {};
Run Code Online (Sandbox Code Playgroud)
然后像Join<Derived_A, Derived_B>
, Join<Derived_A>
, Join<Derived_B>
, and一样使用它Join<>
(不继承任何东西)。