【发布时间】:2016-03-04 12:49:28
【问题描述】:
从 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 ?!?
}
【问题讨论】:
-
试试
using Base<bool>::Base;(我自己没试过,但我确定你需要<bool>在::之前,只是不确定::之后的工作原理) -
你到底想做什么?如果您想要 Base
bool 构造函数,那么您不需要模板化构造函数。然后你只需按照 JSF 的建议说 using Base<bool>::Base。 -
构造函数必须全部继承在一起,不能挑挑拣拣。 (但是,默认、复制和移动构造函数会被自动排除。)
标签: c++ templates c++11 inheritance constructor