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

Vin*_*ent 2 c++ inheritance templates constructor c++11

从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 ?!?
}
Run Code Online (Sandbox Code Playgroud)

vso*_*tco 6

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

using Base<bool>::Base;
Run Code Online (Sandbox Code Playgroud)

Live on Coliru

你不需要Base<bool>之后::,实际上如果你把它放在代码中就不会编译.构造函数仍然被称为Base,而不是Base<bool>.这与引用类模板的成员函数是一致的:您使用eg void Foo<int>::f()而不是void Foo<int>::f<int>()引用Foo的成员函数f().