- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
以下代码无法同时使用 Gcc 和 Clang 编译,因为 B
基类子对象在 A
构造函数中的复制构造:
struct B{
B();
B(const B&) =delete;
};
struct A:B{
A():B(B()){} //=> error: use of deleted function...
};
不过根据 [class.base.init]/7 :
The expression-list or braced-init-list in a mem-initializer is used to initialize the designated subobject (or, in the case of a delegating constructor, the complete class object) according to the initialization rules of [dcl.init] for direct-initialization.
所以初始化规则对于成员或直接基是相同的。对于成员子对象,Gcc 和 Clang 不使用已删除的复制构造函数:
struct A2:B{
B b;
A2():b(B()){} //=> OK the result object of B() is b
};
这不是 Clang 和 Gcc 的编译器错误吗? B
的拷贝构造函数不应该在A()
中省略吗?
有趣的是,即使 gcc 检查复制构造是否格式正确,它也会忽略此复制构造函数调用,参见 assembly here
最佳答案
这确实很奇怪。首先,与是否应在此处调用复制构造函数无关,因为 [class.base.init]/7不区分初始化基和初始化成员,两种情况下的行为应该相同。我在标准中找不到任何额外的措辞,它会以某种方式在基的初始化与成员的初始化之间引入不对称。仅基于这一点,我想说我们可以得出结论,这里或多或少存在编译器错误。 icc 和 MSVC 在初始化基类与初始化成员时似乎至少表现一致。但是,icc 和 MSVC 在代码是否应该被接受的问题上存在分歧。
如果我们查看 [class.base.init]/7,我相信可以找到是否应该调用复制构造函数的问题的答案。再次:
The expression-list or braced-init-list in a mem-initializer is used to initialize the designated subobject (or, in the case of a delegating constructor, the complete class object) according to the initialization rules of [dcl.init] for direct-initialization. […]
强调我的。我相信 [dcl.init] 的相关位应该是 [dcl.init]/17.6 ,我们在哪里找到:
If the destination type is a (possibly cv-qualified) class type:
If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object. […]
Otherwise, if the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. The applicable constructors are enumerated ([over.match.ctor]), and the best one is chosen through overload resolution ([over.match]). […]
[…]
如果要应用 17.6.2,这意味着应该调用复制构造函数,这将使 MSVC 成为本示例中唯一行为正确的主要编译器。但是,我的解释是 17.6.1 一般适用并且 icc 是正确的,即您的代码应该编译。这意味着您在这里所拥有的甚至可能是 GCC 和 clang 中的两个错误(基的初始化与成员的初始化行为不同 + mem-initializer 调用了 copy-ctor,尽管它不应该),还有一个在 MSVC 中……
关于c++ - 复制省略直接基类初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53883664/
经过几个小时的(重新)搜索,我无法想出普通抽象类和使用模板模式之间的可解释区别。 我唯一看到的是: 使用抽象类时,您需要实现所有方法。但是在使用模板方法时,您只需要实现这两个抽象方法。 有人可以向我解
我正在尝试实现一种算法,该算法可找到以下形状给出的外多边形的每个单独边的对应区域。也就是说,1,2 边的相应区域是 [1,6,7,8,2],2,3 边的区域是 [2,8,3] 等等,CCW 或 CW
我正在尝试在派生 self 的 BaseController 类的任何 Controller 上自动设置一个属性。这是我的 Application_Start 方法中的代码。 UnitOfWork 属
我正在使用 mgcv 包通过以下方式将一些多项式样条拟合到一些数据: x.gam smooth$knots [1] -0.081161 -0.054107 -0.027053 0.000001
考虑以下代码: void foo(){ ..... } int main() { int arr[3][3] ; char string[10]; foo();
本书The c++ programming language有这个代码: class BB_ival_slider : public Ival_slider, protected BBslider {
是否有一个 package.json 属性可用于指定模块解析应启动的根文件夹? 例如,假设我们在 node_modules/mypackage/src/file1 中有一个安装。我们要导入的所有文件都
我正在尝试使用聚合函数来实现与 SQL 查询相同的结果: 查询语句: sqldf(" SELECT PhotoID, UserID,
我正在比较使用 LOESS 回归的两条线。我想清楚地显示两条线的置信区间,我遇到了一些困难。 我尝试过使用各种线型和颜色,但在我看来,结果仍然是忙碌和凌乱。我认为置信区间之间的阴影可能会使事情变得更清
给定这段代码 public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFold
我正在构建某种工厂方法,它按以下方式将 DerivedClass 作为 BaseClass 返回: BaseClass Factory() { return DerivedClass(); }
当重写 class delegation 实现的接口(interface)方法时,是否可以调用通常从重写函数中委托(delegate)给的类?类似于使用继承时调用 super 的方式。 来自docum
我有一个基类 fragment (如下所示)。我在其他 3 个 fragment 类中扩展了此类,每个类都共享需要在这 3 个 fragment 中访问的相同 EditText。因此,我在基类中设置了
如何在不加载额外库的情况下在 R 中计算两个排列之间的 Kendall tau 距离(又名冒泡排序距离)? 最佳答案 这是一个 O(n.log(n)) 的实现,在阅读后拼凑而成,但我怀疑可能有更好的
情况 我创建了一个具有国际化 (i18n) 的 Angular 应用程序。我想在子域中托管不同的版本,例如: zh.myexample.com es.myexample.com 问题 当我使用命令 n
std::is_base_of 之间的唯一区别和 std::is_convertible是前者在 Base 时也成立是 私有(private)或 protected Derived 的基类.但是,您何
我创建了一个名为 baseviewcontroller 的父类(super class) uiviewcontroller 类,用于包含大多数应用屏幕所需的基本 UI。它包括一个自定义导航栏和一个“自
我是一名优秀的程序员,十分优秀!