首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

value initialization

这是使用空初始化器构造变量时执行的初始化。

句法

T();

(1)

?

new T ();

(2)

?

Class::Class(...) : member() { ... }

(3)

?

T object {};

(4)

(since C++11)

T{};

(5)

(since C++11)

new T {};

(6)

(since C++11)

Class::Class(...) : member{} { ... }

(7)

(since C++11)

解释

在以下情况下执行值初始化:

1,5%29当创建一个无名的临时对象时,初始化器由一个空的括号组成,或者大括号%28,因为C++11%29;

对象创建具有动态存储持续时间的对象时,则为2,6%29。新表达式使用由空对括号或大括号组成的初始化器(自C++11%29以来);

当非静态数据成员或基类使用成员初始化器自C++11%29以来,使用空的一对括号或大括号%28;

4) when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces.

(since C++11)

在所有情况下,如果空的一对大括号{}被使用并且T集料类型,,,聚合-初始化执行而不是值初始化。

如果T是一个类类型,它没有默认的构造函数,但是构造函数使用std::initializer_list,,,列表初始化被执行。

值初始化的效果如下:

1) if T is a class type with at least one user-provided constructor of any kind, the default constructor is called;

(until 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)

2) if T is a non-union class type without any user-provided constructors, every non-static data member and base-class component of T is value-initialized;

(until C++11)

2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;

(since C++11)

3%29T是数组类型,数组的每个元素都是值初始化的;

4%29否则,对象为零初始化.

注记

构造函数是用户提供如果它是用户声明的,并且没有在第一个声明中显式地默认。

语法T object();不初始化对象;它声明一个不接受参数并返回的函数。T.在C++11之前初始化命名变量的方法是T object = T();,哪个值-初始化一个临时的,然后复制-初始化对象:大多数编译器优化副本在这种情况下。

在C++98之前引入C++03%28,其中引入值初始化%29,表达式new T()被归类为默认初始化和指定的零初始化。

引用不能初始化值。

如上文所述功能铸造,语法T()%281%29被禁止用于数组,而T{}%285%29是允许的。

所有标准集装箱%28std::vector,,,std::list,等等,%29值-用单个值构造元素时初始化它们的元素。size_type争论或通过对...resize()...

自C++11以来,值--初始化一个没有用户提供的构造函数的类,该构造函数具有一个类类型的成员和一个用户提供的构造函数,在调用它的构造函数之前,将成员从零:

二次

代码语言:javascript
复制
struct A
{
    int i;
    A() { } // user-provided default ctor, does not initialize i
};
 
struct B { A a; }; // implicitly-defined default ctor
 
std::cout << B().a.i << '\n'; // value-initializes a B temporary
                              // leaves b.a.i uninitialized in C++03
                              // sets b.a.i to zero in C++11
// (note that B{}.a.i leaves b.a.i uninitialized in C++11, but for 
// a different reason: in post-DR1301 C++11, B{} is aggregate-initialization,
// which then value-initializes A, which has a user-provided ctor)

二次

二次

代码语言:javascript
复制
#include <string>
#include <vector>
#include <iostream>
 
struct T1
{
    int mem1;
    std::string mem2;
}; // implicit default constructor
 
struct T2
{
    int mem1;
    std::string mem2;
    T2(const T2&) { } // user-provided copy constructor
};                    // no default constructor
 
struct T3
{
    int mem1;
    std::string mem2;
    T3() { } // user-provided default constructor
};
 
std::string s{}; // class => default-initialization, the value is ""
 
int main()
{
    int n{};                // scalar => zero-initialization, the value is 0
    double f = double();    // scalar => zero-initialization, the value is 0.0
    int* a = new int[10](); // array => value-initialization of each element
                            //          the value of each element is 0
    T1 t1{};                // class with implicit default constructor =>
                            //     t1.mem1 is zero-initialized, the value is 0
                            //     t1.mem2 is default-initialized, the value is ""
//  T2 t2{};                // error: class with no default constructor
    T3 t3{};                // class with user-provided default constructor =>
                            //     t3.mem1 is default-initialized to indeterminate value
                            //     t3.mem2 is default-initialized, the value is ""
    std::vector<int> v(3);  // value-initialization of each element
                            // the value of each element is 0
    std::cout << s.size() << ' ' << n << ' ' << f << ' ' << a[9] << ' ' << v[2] << '\n';
    std::cout << t1.mem1 << ' ' << t3.mem1 << '\n';
    delete[] a;
}

二次

可能的产出:

二次

代码语言:javascript
复制
0 0 0 0 0
0 4199376

二次

缺陷报告

以下行为更改缺陷报告追溯应用于先前发布的C++标准。

DR

Applied to

Behavior as published

Correct behavior

CWG 1301

C++11

defaulted default constructor skipped zero-init before construction

zero-init performed

另见

  • 默认初始化
  • 直接初始化
  • 复制初始化
  • 列表初始化
代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com