Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is inheriting constructors not supported by ALL of the existing C++ compilers?

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)
like image 385
xmllmx Avatar asked Sep 06 '25 21:09

xmllmx


1 Answers

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.

like image 130
Ben Voigt Avatar answered Sep 08 '25 16:09

Ben Voigt