Vincent
Vincent

Reputation: 60341

Inheriting templated constructor from class template?

What would be the syntax to inherit constructors (some of them are template) from a class template in 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 ?!?
}

Upvotes: 3

Views: 2048

Answers (1)

vsoftco
vsoftco

Reputation: 56547

You derive from Base<bool>. So your base class is Base<bool>, and inheriting the constructors is done via

using Base<bool>::Base;

Live on Coliru

You do not need Base<bool> after the ::, in fact the code doesn't compile if you put it. The constructors are still referred to as Base, and not Base<bool>. This is consistent with referring to member functions of class templates: you use e.g. void Foo<int>::f() and not void Foo<int>::f<int>() to refer to the Foo's member function f().

Upvotes: 8

Related Questions