C++ 11 Feature Proposal: Inherited constructors

189 views
Skip to first unread message

Matthew Dempsky

unread,
Oct 29, 2014, 7:06:01 PM10/29/14
to Chromium-dev
C++11 adds a way to explicitly inherit base class constructors; e.g.:

    class Base {
     public:
      Base(int, double) { ... }
      Base(char*) { ... }
      ...
    };

    class Derived : public Base {
     public:
      // Similar to explicitly defining:
      //     Derived(int x, double y) : Base(x, y) {}
      //     Derived(char* z) : Base(z) {}
      using Base::Base;
      ...
    };

I'd like to see them allowed by the Chromium C++ style guide, but they're currently not listed at all on chromium-cpp.appspot.com.

My use case is I have some typedefs for a class, but they're purely an implementation detail, so it seems unfortunate to need to define them in the header.  As an alternative, I've found this seems to work nicely:

    foo.h:
        class Foo {
          ...
         private:
          struct FooKey;
          FooKey MakeKey(...);
          ...
        };

    foo.cc:
        #include "base/tuple.h"

        struct Foo::FooKey : public Tuple4<X, Y, Z, W> {
          using Tuple4<X, Y, Z, W>::Tuple4;
        };

        ...

Then I don't need to include base/tuple.h or any headers necessary for declaring X/Y/Z/W in the header, and I don't need to add boilerplate passthrough constructors myself.

Alex Vakulenko

unread,
Oct 29, 2014, 7:08:52 PM10/29/14
to Matthew Dempsky, Chromium-dev
The feature is called "Delegated Constructors" and we have a discussion thread for it: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/0zVA8Ctx3Xo

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

Alex Vakulenko

unread,
Oct 29, 2014, 7:10:24 PM10/29/14
to Matthew Dempsky, Chromium-dev
Oh, sorry, I jumped the gun. You are talking about something completely different... Apologies.

Jeffrey Yasskin

unread,
Oct 29, 2014, 7:13:48 PM10/29/14
to Alex Vakulenko, Matthew Dempsky, Chromium-dev
Delegating and Inheriting constructors are different features.
However, http://msdn.microsoft.com/en-us/library/hh567368.aspx says
MSVC 2013 doesn't implement inheriting constructors, so they're out
for now.

On Wed, Oct 29, 2014 at 4:08 PM, Alex Vakulenko <avaku...@chromium.org> wrote:

Nico Weber

unread,
Oct 29, 2014, 7:18:03 PM10/29/14
to Jeffrey Yasskin, Alex Vakulenko, Matthew Dempsky, Chromium-dev
Matthew, can you send a patch to add a row for them (in the banned section for now, it seems)?

Matthew Dempsky

unread,
Oct 29, 2014, 7:20:15 PM10/29/14
to Jeffrey Yasskin, Alex Vakulenko, Chromium-dev
On Wed, Oct 29, 2014 at 4:13 PM, Jeffrey Yasskin <jyas...@chromium.org> wrote:
However, http://msdn.microsoft.com/en-us/library/hh567368.aspx says
MSVC 2013 doesn't implement inheriting constructors, so they're out
for now.

Matthew Dempsky

unread,
Oct 31, 2014, 9:18:50 PM10/31/14
to Nico Weber, Jeffrey Yasskin, Alex Vakulenko, Chromium-dev
Inherited constructors are now listed as banned in chromium-cpp.appspot.com, but I'm interested if anyone can suggest better wording and/or a better documentation link for the entry.  In a recent CL, I added a "using Base::Member;" declaration, and my reviewer thought the style guide disallowed *all* "using Base::X;" declarations, not simply the special case of "using Base::Base;".

Etienne Pierre-doray

unread,
Jul 29, 2016, 11:46:00 AM7/29/16
to Chromium-dev, tha...@chromium.org, jyas...@chromium.org, avaku...@chromium.org
https://msdn.microsoft.com/en-us/library/hh567368.aspx states that MSVC2015 supports inherited constructor.
The following code compile and execute correctly using MSVS2015:

class Base {
 public:
  Base(int) {}
  Base(double) {}
};

class Derived : public Base {
 public:
  using Base::Base;
};

int main() {
  Derived d1(3);
  Derived d2(3.0);
}

It seems like the reason for their ban was that MSVC2013 didn't implement them. I'd like to see them allowed now that MSVC2015 supports them.

Nico Weber

unread,
Jul 29, 2016, 1:15:27 PM7/29/16
to Etienne Pierre-doray, Chromium-dev, Jeffrey Yasskin, Alex Vakulenko
Sounds fine to me. Please send a CL that adds a use of that (to show that it works) and that moves the feature to the "allowed" list.

Jeremy Roman

unread,
Jul 29, 2016, 11:52:54 PM7/29/16
to Nico Weber, Etienne Pierre-doray, Chromium-dev, Jeffrey Yasskin, Alex Vakulenko, cxx

Peter Kasting

unread,
Nov 3, 2016, 6:57:21 PM11/3/16
to etie...@google.com, Chromium-dev, Nico Weber, Jeffrey Yasskin, Alex Vakulenko
On Fri, Jul 29, 2016 at 8:46 AM, 'Etienne Pierre-doray' via Chromium-dev <chromi...@chromium.org> wrote:
It seems like the reason for their ban was that MSVC2013 didn't implement them. I'd like to see them allowed now that MSVC2015 supports them.

Reply all
Reply to author
Forward
0 new messages