在 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;
你不需要Base<bool>
在::
之后,实际上,如果您输入代码,则无法编译。构造函数仍称为 Base
,而不是 Base<bool>
.这与引用类模板的成员函数是一致的:你使用例如void Foo<int>::f()
而不是 void Foo<int>::f<int>()
引用Foo
的成员函数 f()
.
关于c++ - 从类模板继承模板化构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34006149/