Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?!?
}
like image 894
Vincent Avatar asked Feb 07 '26 11:02

Vincent


1 Answers

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().

like image 134
vsoftco Avatar answered Feb 09 '26 01:02

vsoftco