没有默认构造函数的类对象的值初始化 [英] Value initialization of class object without default constructor

查看:462
本文介绍了没有默认构造函数的类对象的值初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解C ++ 11中类类型TT()T{}值初始化的确切行为. 让我感到困惑的是这两个摘录,摘录自 http://en.cppreference.com :

I am trying to understand the exact behavior of value initialization by T() or T{} for a class type T in C++11. What confuses me are these two snippets taken from http://en.cppreference.com:

值初始化:

值初始化的影响是: [...]

The effects of value initialization are: [...]

1)如果T是具有没有默认构造函数或具有用户提供或删除的默认构造函数的类类型,则该对象将被默认初始化; (自C ++ 11起)

1) if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized; (since C++11)

[...]

所以我查找了默认初始化:

默认初始化的效果是:

The effects of default initialization are:

  • 如果T是类类型,则考虑构造函数,并针对空参数列表对其进行重载解析.调用选定的构造函数(默认构造函数之一)为新对象提供初始值;
  • if T is a [...] class type, the constructors are considered and subjected to overload resolution against the empty argument list. The constructor selected (which is one of the default constructors) is called to provide the initial value for the new object;

[...]

因此,这基本上表明,如果T是一个类类型,并且其隐式默认构造函数不可用,那么将通过调用其默认构造函数之一来构造该对象?以我的理解,这对于用户提供的默认构造函数的上述情况才有意义;然后,在构造时,将仅执行该构造函数中明确声明的内容,并且未明确初始化的每个成员都将被默认初始化(如果我错了,请纠正我).

So this basically says that if T is a class type and its implicit default constructor is not available, then the object will be constructed by a call to one of its default constructors? In my understanding, this makes only sense for the mentioned case of a user-provided default constructor; then, upon construction, only what is explicitly stated in that constructor will be executed, and every member not explicitly initialized will get default-initialized (please correct me if I am wrong here).

我的问题:

1)如果没有用户提供的默认构造函数,并且没有默认构造函数或将其删除,会发生什么情况?我猜代码不会编译.如果正确,那么:

1) What would happen if there was no user-provided default constructor and there was no default constructor or it was deleted? I would guess the code would not compile. If this is right, then:

2)还需要明确提及无默认构造函数"和删除默认构造函数"的情况吗?

2) What is the need to also explicitly mention the cases "no default constructor" and "deleted default constructor"?

推荐答案

cppreference上的措辞似乎与标准中的措辞不符. C ++ 11 8.5/7 [dcl.init]:

The wording on cppreference doesn't seem to match that in the standard. C++11 8.5/7 [dcl.init]:

值初始化类型为T的对象的意思是:

To value-initialize an object of type T means:

  • 如果T是具有用户提供的构造函数(12.1)的(可能是cv限定的)类类型(第9条),则 T的默认构造函数被调用(如果T没有可访问的默认值,则初始化格式不正确. 构造函数);
  • 如果T是没有用户提供的构造函数的(可能是cv限定的)非工会类类型,则该对象 是零初始化的,并且,如果T的隐式声明的默认构造函数很重要,则该构造函数为 叫.
  • 如果T是数组类型,则每个元素都将被值初始化;
  • 否则,该对象将被初始化为零.
  • if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.
  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized.

价值初始化的对象被认为是构造的,因此要遵守本国际公约的规定 适用于构造的"对象,构造函数已完成"的对象等的标准, 即使没有为该对象的初始化调用任何构造函数.

An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to "constructed" objects, objects "for which the constructor has completed," etc., even if no constructor is invoked for the object’s initialization.

为了进行比较,这是C ++ 14(n4140)8.5/7 [dcl.init]中的措辞:

For comparison, this is the wording in C++14 (n4140) 8.5/7 [dcl.init]:

值初始化类型为T的对象的意思是:

To value-initialize an object of type T means:

  • 如果T是(可能具有cv限定的)类类型(第9条),没有默认构造函数(12.1)或 用户提供或删除的默认构造函数,然后该对象将被默认初始化;
  • 如果T是(可能是cv限定的)类类型,但没有用户提供或删除的默认构造函数,则 将该对象初始化为零,并检查默认初始化的语义约束,如果 T具有非平凡的默认构造函数,该对象是默认初始化的;
  • 如果T是数组类型,则每个元素都将被值初始化;
  • 否则,该对象将被初始化为零.
  • if T is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a default constructor that is user-provided or deleted, then the object is default-initialized;
  • 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 if T has a non-trivial default constructor, the object is default-initialized;
  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized.

价值初始化的对象被认为是构造的,因此要遵守本国际公约的规定 适用于构造的"对象,构造函数已完成"的对象等的标准, 即使没有为该对象的初始化调用任何构造函数.

An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to "constructed" objects, objects "for which the constructor has completed," etc., even if no constructor is invoked for the object’s initialization.

这篇关于没有默认构造函数的类对象的值初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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