c++ - 从依赖模板的基类继承构造函数

标签 c++ templates inheritance

如果基类的名称依赖于(派生类的)模板参数,我如何继承基类的构造函数?

示例:

#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::AB::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;
};

LIVE

关于c++ - 从依赖模板的基类继承构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67385604/

相关文章:

c# - HiddenField 不是 WebControl?

kotlin - kotlin,该函数在基类的init block 中调用

c++ - 理解通用引用的类型推导

带有模板的c++类找不到它的构造函数

c# - Client Generated 不向第三方提供有关已知类型的信息

c++ - 当 Py_initialize 失败时如何捕获并处理 fatal error ?

powershell - .NET Core 3.0预览版中缺少blazorwasm模板

c++ - 使用文件流时,如何使程序无论用户的密码如何都可以运行?

c++ - 奇怪的多重定义错误

c++ - 将指向 char* 数组的指针传递给函数以在 Arduino 上操作值