- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我不明白 C++11 大括号初始化规则在这里是如何工作的。拥有此代码:
struct Position_pod {
int x,y,z;
};
class Position {
public:
Position(int x=0, int y=0, int z=0):x(x),y(y),z(z){}
int x,y,z;
};
struct text_descriptor {
int id;
Position_pod pos;
const int &constNum;
};
struct text_descriptor td[3] = {
{0, {465,223}, 123},
{1, {465,262}, 123},
};
int main()
{
return 0;
}
注意,数组被声明为有 3 个元素,但只提供了 2 个初始化器。
但是它编译没有错误,这听起来很奇怪,因为最后一个数组元素的引用成员将未初始化。确实,它有 NULL 值:
(gdb) p td[2].constNum
$2 = (const int &) @0x0: <error reading variable>
现在是“魔法”:我将 Position_pod 更改为 Position
struct text_descriptor {
int id;
Position_pod pos;
const int &constNum;
};
变成这样:
struct text_descriptor {
int id;
Position pos;
const int &constNum;
};
现在它给出了预期的错误:
error: uninitialized const member ‘text_descriptor::constNum'
我的问题:为什么它在第一种情况下编译,什么时候应该给出错误(如在第二种情况下)。不同的是,Position_pod 使用 C 风格的大括号初始化,而 Position 使用 C++11 风格的初始化,调用 Position 的构造函数。但这会如何影响不初始化引用成员的可能性呢?
(更新)编译器:gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
最佳答案
很明显
struct text_descriptor td[3] = {
{0, {465,223}, 123},
{1, {465,262}, 123},
};
是列表初始化,并且初始化列表不为空。
C++11 说 ([dcl.init.list]p3):
List-initialization of an object or reference of type
T
is defined as follows:
- If the initializer list has no elements and
T
is a class type with a default constructor, the object is value-initialized.- Otherwise, if
T
is an aggregate, aggregate initialization is performed (8.5.1).- ...
[dcl.init.aggr]p1:
An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).
td
是数组,所以是聚合,所以进行聚合初始化。
[dcl.init.aggr]p7:
If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from an empty initializer list (8.5.4).
这里就是这种情况,所以 td[2]
是从一个空的初始化列表初始化的,这(再次是 [dcl.init.list]p3)意味着它是值初始化的。
反过来,值初始化意味着 ([dcl.init]p7):
To value-initialize an object of type
T
means:
- if
T
is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), ...- if
T
is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, ifT
's implicitly-declared default constructor is non-trivial, that constructor is called.- ...
你的类 text_descriptor
是一个没有用户提供构造函数的类,所以 td[2]
首先是零初始化,然后调用它的构造函数。
零初始化意味着([dcl.init]p5):
To zero-initialize an object or reference of type T means:
- if
T
is a scalar type (3.9), ...- if
T
is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;- if
T
is a (possibly cv-qualified) union type, ...- if
T
is an array type, ...- if
T
is a reference type, no initialization is performed.
无论 text_descriptor
的默认构造函数如何,这都是明确定义的:它只是将非引用成员和子成员初始化为零。
然后调用默认构造函数,如果它是非平凡的。下面是默认构造函数的定义方式([special]p5):
A default constructor for a class
X
is a constructor of classX
that can be called without an argument. If there is no user-declared constructor for classX
, a constructor having no parameters is implicitly declared as defaulted (8.4). An implicitly-declared default constructor is an inline public member of its class. A defaulted default constructor for classX
is defined as deleted if:
- ...
- any non-static data member with no brace-or-equal-initializer is of reference type,
- ...
A default constructor is trivial if it is not user-provided and if:
- its class has no virtual functions (10.3) and no virtual base classes (10.1), and
- no non-static data member of its class has a brace-or-equal-initializer, and
- all the direct base classes of its class have trivial default constructors, and
- for all the non-static data members of its class that are of class type (or array thereof), each such class has a trivial default constructor.
Otherwise, the default constructor is non-trivial.
因此,隐式定义的构造函数被删除,正如预期的那样,但是如果 pos
是 POD 类型(!),它也是微不足道的。因为构造函数是微不足道的,所以它没有被调用。因为构造函数没有被调用,所以被删除是没有问题的。
这是 C++11 中的一个大漏洞,现已修复。碰巧已经修复处理inaccessible trivial default constructors ,但固定的措辞也涵盖已删除的琐碎默认构造函数。 N4140(大约 C++14)在 [dcl.init.aggr]p7(强调我的)中说:
- if
T
is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized and the semantic constraints for default-initialization are checked, and ifT
has a non-trivial default constructor, the object is default-initialized;
作为 T.C.在评论中指出,another DR也进行了更改,以便 td[2]
仍然从一个空的初始化程序列表进行初始化,但是该空的初始化程序列表现在意味着聚合初始化。反过来,这意味着 td[2]
的每个成员也是从一个空的初始化程序列表初始化的(再次是 [dcl.init.aggr]p7),所以似乎初始化了引用{}
的成员。
[dcl.init.aggr]p9 然后说(正如 remyabel 在现已删除的答案中指出的那样):
If an incomplete or empty initializer-list leaves a member of reference type uninitialized, the program is ill-formed.
我不清楚这是否适用于从隐式 {}
初始化的引用,但编译器确实会这样解释它,而且它没有太多其他含义。
关于C++11奇怪的大括号初始化行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408911/
我是 Spring 新手,这就是我想要做的事情: 我正在使用一个基于 Maven 的库,它有自己的 Spring 上下文和 Autowiring 字段。 它的bean配置文件是src/test/res
我在我的测试脚本中有以下列表初始化: newSequenceCore=["ls", "ns", "*", "cm", "*", "ov", "ov", "ov", "ov", "kd"] (代表要在控
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Class construction with initial values 当我查看 http://en.
我得到了成员变量“objectCount”的限定错误。编译器还返回“ISO C++ 禁止非常量静态成员的类内初始化”。这是主类: #include #include "Tree.h" using n
我有如下所示的a.h class A { public: void doSomething()=0; }; 然后我有如下所示的b.h #include "a.h" class b: publi
我需要解析 Firebase DataSnapshot (一个 JSON 对象)转换成一个数据类,其属性包括 enum 和 list。所以我更喜欢通过传递 DataSnapshot 来手动解析它进入二
我使用 JQuery 一段时间了,我总是使用以下代码来初始化我的 javascript: $(document).ready( function() { // Initalisation logic
这里是 Objective-C 菜鸟。 为什么会这样: NSString *myString = [NSString alloc]; [myString initWithFormat:@"%f", s
我无法让核心数据支持的 NSArrayController 在我的代码中正常工作。下面是我的代码: pageArrayController = [[NSArrayController alloc] i
我对这一切都很陌生,并且无法将其安装到我的后端代码中。它去哪里?在我的页脚下面有我所有的 JS? 比如,这是什么意思: Popup initialization code should be exec
这可能是一个简单的问题,但是嘿,我是初学者。 所以我创建了一个程序来计算一些东西,它目前正在控制台中运行。我决定向其中添加一个用户界面,因此我使用 NetBeans IDE 中的内置功能创建了一个 J
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
据我所知, dependentObservable 在声明时会进行计算。但如果某些值尚不存在怎么办? 例如: var viewModel ={}; var dependentObservable1 =
我正在阅读 POODR 这本书,它使用旧语法进行默认值初始化。我想用新语法实现相同的功能。 class Gear attr_reader :chainring, :cog, :wheel de
我按照 polymer 教程的说明进行操作: https://www.polymer-project.org/3.0/start/install-3-0 (我跳过了可选部分) 但是,在我执行命令“po
很抱歉问到一个非常新手的Kotlin问题,但是我正在努力理解与构造函数和初始化有关的一些东西。 我有这个类和构造函数: class TestCaseBuilder constructor(
假设我们有一个包含 30 列和 30 行的网格。 生命游戏规则简而言之: 一个小区有八个相邻小区 当一个细胞拥有三个存活的相邻细胞时,该细胞就会存活 如果一个细胞恰好有两个或三个活的相邻细胞,那么它就
我是 MQTT 和 Android 开放附件“AOA” 的新手。在阅读教程时,我意识到,在尝试写入 ByteArrayOutputStream 类型的变量之前,应该写入 0 或 0x00首先到该变量。
我有 2 个 Controller ,TEST1Controller 和 TEST2Controller 在TEST2Controller中,我有一个initialize()函数设置属性值。 如果我尝
我有一个inotify /内核问题。我正在使用“inotify” Python项目进行观察,但是,我的问题仍然是固有的关于inotify内核实现的核心。 Python inotify项目处理递归ino
我是一名优秀的程序员,十分优秀!