如果基类的名称依赖于(派生类的)模板参数,我如何继承基类的构造函数?
示例:
#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
。
该程序在编写时无法编译。
正确的语法是什么?
最佳答案
您可以先定义一个类型别名。
template <bool Abase>
struct C : public std::conditional_t<Abase, A, B> {
using base = std::conditional_t<Abase, A, B>;
using base::base;
};
关于c++ - 从依赖模板的基类继承构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67385604/