7

At first sight, using imports a specific function (such as using std::cout to the scope). But this using imports all the base class constructors to the derived class.

template< typename T >
class Vec : public std::vector< T >
{
public:
    using std::vector<T>::vector;  // ???

    //...
};

What's actually behind the scenes of this `using` declaration?
4
  • There's nothing "behind" it. That's what it does. Commented Jul 19, 2019 at 4:11
  • When you ask What's actually behind the scene, are you trying to find out why the standard defines it that way, or are you trying to find out where it is useful? Commented Jul 19, 2019 at 4:57
  • 1
    Perhaps it just looks a bit different from what I've used and understood. It imports all constructors (instead of a specific function). Also, as it's public inherited, supposedly all the base class constructors should have been available already (ie why need using). thanks Commented Jul 19, 2019 at 5:06
  • [...] should have been available already [...] -- @artm As I observed, semantically equivalent constructors that take arguments are not available through the name of the derived class. Commented Jun 8, 2023 at 12:19

1 Answer 1

7

As it's public inherited, supposedly all the base class constructors should have been available already (ie why need using)?

No, constructors of the base class are not inherited by default. A detailed explanation can be found in the following discussions:


What's actually behind the scene of this using declaration?

From cppreference.com, using does

  1. Using-declarations can be used to introduce namespace members into other namespaces and block scopes, or to introduce base class members into derived class definitions.
  2. [...] ( specific...)

Also while inheritance:

If the using-declaration refers to a constructor of a direct base of the class being defined (e.g. using Base::Base;), all constructors of that base (ignoring member access) are made visible to overload resolution when initializing the derived class.

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

Comments

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.