4

For now, both of G++ and VC++ 2010 don't support inheriting constructors.

However, I think this is one the most beautiful features in C++0x. And I think it should be rather easy to implement by the compiler.

Why are the compilers not interested of this feature?

Assume I want to design my own string class by inheriting std::string like this:

class MyString : public std::string
{
public:
// I have to redefine many overloaded ctors here and forward their arguments to 
// std::string's ctors. How tedious it will be!!!
};

A beautiful code example:

struct B1 
{
   B1(char);
};

struct B2 
{
   B2(double);
   B2(int);
};

struct D1 : B1, B2 
{
   using B1::B1; //  D1(char)
   using B2::B2;  // D1(double), D1(int)
};

D1 d('c'); //OK, invokes D1(char)
xmllmx
  • 39,765
  • 26
  • 162
  • 323

5 Answers5

7

There's a lot of new material in C++0x, and it seems that the volunteers working on gcc found other changes more interesting to work on first.

As for VC++, there's not only the prioritization of work, but the additional costs (often mentioned on Microsoft product manager blogs) of requirements, documentation, and very extensive testing, which are necessary parts of a product that is sold, above and beyond just making it work.

Finally, it sounds like there was recent discussion among the C++0x committee about cutting this feature, since something like 95% of use cases have straightforward workarounds.

Combined, I'm not surprised that the compiler engineers are saving this one for later.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

I don't think you're going to find a specific rationale for them not picking up this feature yet. The general principle is, there are many other things in C++0x, and compiler folks are generally much more eager to implement library features than features of the language, particularly when the standard isn't done yet and the language features might change.

zwol
  • 135,547
  • 38
  • 252
  • 361
  • 1
    Conversely, many of the library changes *can't* be done until the language support is there (and the libraries also have to change if language features change). – Ben Voigt Dec 11 '10 at 17:37
2
  1. C++0x is not a standard yet.
  2. They haven't enough time to implement it.
  3. It's not so simple as you think. It's not enough to just call these constructors. The compiler need to generate appropriate constructors in the derived class in order to initialize the members of the derived class.
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
1

It is March 2012 and still no compilers support this.

A variadic template constructor can wrap base class constructors nicely, and would let you override certain ctors to boot. This may not work well for your multiple inheritance example.

class MyString: public std::string
{
public:
    template<class ...Args>
    MyString(Args... args): std::string(args...){}
};
goertzenator
  • 1,960
  • 18
  • 28
0

The compilers will only implement the features of a specific language version. If a compiler says to support C++0x and doesn't support all its features, then it's the compilers fault, use a better compiler.

I don't think C++0x is implemented in the compilers you mentioned yet.

Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
  • 1
    As of right now, there is no compiler that actually implements the entire C++0x feature set reasonably. Besides, the C++0x standard hasn't been finalized yet. They are being rolled out gradually. – In silico Dec 11 '10 at 17:08