【发布时间】:2021-07-26 20:23:08
【问题描述】:
如果基类的名称依赖于(派生类的)模板参数,我如何继承基类的构造函数?
例子:
#include <type_traits>
struct A {
};
struct B {
};
template <bool Abase>
struct C : public std::conditional_t<Abase, A, B> {
using std::conditional_t<Abase, A, B>::(std::conditional_t<Abase, A, B>); // Error!
}
int main()
{
C<true> c;
C<false> c2;
}
在程序中struct C应该继承A::A或B::B,取决于模板参数bool Abase的值。
编写的程序无法编译。
正确的语法是什么?
【问题讨论】:
-
using std::conditional_t<Abase, A, B>::conditional_t;有效(并且是standard),虽然它很奇怪。
标签: c++ templates inheritance