在C ++中继承构造函数有多么有用? [英] How useful would Inheriting Constructors be in C++?

查看:106
本文介绍了在C ++中继承构造函数有多么有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我坐在C ++标准委员会会议上,他们正在讨论放弃继承构造函数,因为没有编译器供应商实现它(用户没有一直在请求它)。



让我很快提醒每个人继承的构造函数是什么:

  struct B 
{
B
};

struct D:B
{
使用B :: B;
};

一些供应商提议使用r值引用和可变参数模板



例如:

  struct D:B 
{
template< class ... Args>
D(Args& ... args):B(args ...){}
};

我有两个问题:



1 )你能够从你的编程经验中提供真正的世界(非设计)的例子,这将从继承构造函数中获益匪浅吗?



2)有什么技术原因

解决方案

/ div>


2)有没有任何技术原因,你可以认为这将阻止完美的转发构造函数作为一个适当的替代?


我在这里展示了一个完美转发方法的一个问题:转发C ++ 0x中的所有构造函数



此外,完美的转发方法不能转发基类构造函数的显式性:它总是一个转换构造函数或从不,



另一个问题是初始化列表构造函数,因为你不能推导出 Args initializer_list< U> 。相反,你需要使用 B {args ...} (注意大括号)并初始化 D 对象与(a,b,c) {1,2,3} = {1,2,3} 。在这种情况下, Args 将是初始化器列表的元素类型,并将它们转发到基类。初始化列表构造函数然后可以接收它们。这似乎导致不必要的代码膨胀,因为模板参数包可能包含许多类型和长度的不同组合的类型序列,并且因为您必须选择初始化语法,这意味着:

  struct MyList {
//通过初始化器列表初始化
MyList(std :: initializer_list< Data> list);

//初始化为大小副本def
MyList(std :: size_t size,Data def = Data());
};

MyList m {3,1}; // data:[3,1]
MyList m(3,1); // data:[1,1,1]

//你使用{args ...}并支持初始化列表或
//使用(args ...)和不会
struct MyDerivedList:MyList {
template< class ... Args>
MyDerivedList(args& ... args):MyList {args ...} {}
};

MyDerivedList m {3,1}; // data:[3,1]
MyDerivedList m(3,1); // data:[3,1](!!)


As I sit in the C++ Standards committee meetings, they are discussing the pros and cons of dropping Inheriting Constructors since no compiler vendor has implemented it yet (the sense being users haven't been asking for it).

Let me quickly remind everyone what inheriting constructors are:

struct B
{
   B(int);
};

struct D : B
{
  using B::B;
};

Some vendors are proposing that with r-value references and variadic templates (perfect forwarding constructors), it would be trivial to provide a forwarding constructor in the inheriting class that would obviate inheriting constructors.

For e.g.:

struct D : B
{
  template<class ... Args> 
    D(Args&& ... args) : B(args...) { } 
};

I have two questions:

1) Can you provide real world (non-contrived) examples from your programming experience that would benefit significantly from inheriting constructors?

2) Are there any technical reasons you can think of that would preclude "perfect forwarding constructors" from being an adequate alternative?

Thanks!

解决方案

2) Are there any technical reasons you can think of that would preclude "perfect forwarding constructors" from being an adequate alternative?

I have shown one problem with that perfect forwarding approach here: Forwarding all constructors in C++0x .

Also, the perfect forwarding approach can't "forward" the expliciteness of base-class constructors: Either it is always a converting constructor or never, and the base-class will always be direct initialized (always making use of all constructors, even explicit ones).

Another problem are initializer-list constructors because you can't deduce Args to initializer_list<U>. Instead, you would need to forward to the base with B{args...} (note the braces) and initialize D objects with (a, b, c) or {1, 2, 3} or = {1, 2, 3}. In that case, Args would be the element types of the initializer list, and forward them to the base class. A initializer-list constructor can then receive them. This seems to cause unnecessary code bloat because the template argument pack will potentially contain lots of type sequences for each different combination of types and length and because you have to choose an initialization syntax this means:

struct MyList {
  // initializes by initializer list
  MyList(std::initializer_list<Data> list);

  // initializes with size copies of def
  MyList(std::size_t size, Data def = Data());
};

MyList m{3, 1}; // data: [3, 1]
MyList m(3, 1); // data: [1, 1, 1]

// either you use { args ... } and support initializer lists or
// you use (args...) and won't
struct MyDerivedList : MyList {
  template<class ... Args> 
  MyDerivedList(Args&& ... args) : MyList{ args... } { } 
};

MyDerivedList m{3, 1}; // data: [3, 1]
MyDerivedList m(3, 1); // data: [3, 1] (!!)

这篇关于在C ++中继承构造函数有多么有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆