2

i know that you can use constructors from a base class in a derived class like

class A {
public:
    A() {};
}

class B : public A {
public:
    using A::A;
}

Furthermore you can use a constructor from a template base class like

template<typename T>
class A {
public:
    A() {};
}

template<typename T>
class B : public A<T> {
public:
    using A<T>::A;
}

Suppose that class A now has a template function as constructor:

template<typename T1>
class A {
public:
    template<typename T2>
    A() {};
}

template<typename T1>
class B : public A<T1> {
public:
    using A<T1>::A;                           // nope
    using A<T1>::A<>;                         // neither             
    template<typename T2> using A<T1>::A<T2>; // sounds good, doesn't work

}

How could you use the base class constructor in the derived class?

1
  • How would you call that constructor? Commented Jul 13, 2021 at 19:36

1 Answer 1

2

Two things:

  • template<typename T2> A() {} is unusuable as a constructor, since there is no way to deduce T2.
  • You can only inherit all constructors at once. You can't choose specific ones.

Other than that, using A<T1>::A; is correct.

Sign up to request clarification or add additional context in comments.

2 Comments

You're totally right, i've edited the signature of the constructor in the last code snippet so that T2 can be deduced. Well, that's easier than i thought it was. Certainly there was another problem with my more complex code, tested your answer with this minimal sample and it works well! Thanks :)
@syntaxerror147 You're welcome! Usually it's discouraged to make significant changes to the question after getting an answer, to avoid confusing future readers. So I've rolled back the question edit, I hope you don't mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.