c++ - 从类模板继承模板化构造函数?

标签 c++ templates c++11 inheritance constructor

在 C++11 中从类模板继承构造函数(其中一些是模板)的语法是什么?

template <class T>
struct Base
{
    using type = T;
    explicit constexpr Base(const type& x): value{x} {};
    template <class U> explicit constexpr Base(U&& x): value{std::forward<U>(x)} {};
    type value;
}

struct Derived: Base<bool>
{
    using Base::Base<bool>; // Does not seem to work ?!?
}

最佳答案

您派生自 Base<bool> .所以你的基类是 Base<bool> , 继承构造函数是通过

using Base<bool>::Base;

Live on Coliru

你不需要Base<bool>::之后,实际上,如果您输入代码,则无法编译。构造函数仍称为 Base ,而不是 Base<bool> .这与引用类模板的成员函数是一致的:你使用例如void Foo<int>::f()而不是 void Foo<int>::f<int>()引用Foo的成员函数 f() .

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

相关文章:

c++ - 移动构造函数似乎没有执行

c++ - 替换预处理器定义中的字符

c++ - 将现有的 C++ 代码移植到 R

c++ - 通常对函数名的非限定查找与对变量名的查找不同吗?

c++ - 使用 qsort() 进行稳定排序?

c++ - VS2015 std::async 奇怪

C++:具有比较运算符重载的模板类

c++ - 调用了错误的模板函数

c++ - 为什么 C++ 更喜欢这个模板方法而不是方法重载?

c++ - 如何将 const 成员函数作为非常量成员函数传递