问题描述
现在,G ++和VC ++ 2010都不支持继承构造函数。
但是,我认为这是C ++中最漂亮的功能0x 。我认为编译器应该很容易实现。
为什么编译器对这个功能不感兴趣?
假设我想通过继承std :: string来设计我自己的字符串类,如下所示:
class MyString:public std :: string
{
public:
//我必须在这里重新定义许多重载的转换器,并将它们的参数转发给
// std :: string的转换器。如何乏味将是!
};
一个漂亮的代码示例:
struct B1
{
B1(char);
};
struct B2
{
B2(双);
B2(int);
};
struct D1:B1,B2
{
使用B1 :: B1; // D1(char)
using B2 :: B2; // D1(double),D1(int)
};
D1 d('c'); // OK,invokes D1(char)
很多新材料在C ++ 0x,似乎工作在gcc的志愿者发现其他变化更有趣的工作在第一。
对于VC ++,只有工作的优先级,但是额外的成本(通常在Microsoft产品经理博客上提到)需求,文档和非常广泛的测试,这是销售的产品的必要部分,超出只是使其工作。 p>
最后,,因为像95%的用例有直接的解决方法。
组合,我不感到惊讶,编译器工程师以后保存这个。
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)
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.
这篇关于为什么所有现有的C ++编译器不支持继承构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!