inherit constructor from template-dependent base class

Viewed 45

How can I inherit the constructor of a base class, if its name depend on a template parameter (of the derived class)?

Example:

#include <type_traits>

struct A {
};

struct B {
};

template <bool Abase>
struct C : public std::conditional_t<Abase, A, B> {
    using std::conditional_t<Abase, A, B>::(std::conditional_t<Abase, A, B>); // Error!
}

int main()
{
    C<true> c;
    C<false> c2;
}

In the program struct C should inherit A::A or B::B, depending on the value of the template parameter bool Abase. The program, as it is written, does not compile.

What is the correct syntax?

1 Answers

You can define a type alias firstly.

template <bool Abase>
struct C : public std::conditional_t<Abase, A, B> {
    using base = std::conditional_t<Abase, A, B>;
    using base::base;
};

LIVE

Related