gpt4 book ai didi

c++ - 大括号初始化没有任何值

转载 作者:太空狗 更新时间:2023-10-29 23:39:37 28 4
gpt4 key购买 nike

这样写可以吗

typedef unsigned long DWORD;
DWORD nBytesRead = {};

这个变量在这个表达式后面会包含0吗?

最佳答案

是的,没关系,你保证 nBytesRead 将包含值零。您正在使用空的初始化列表复制初始化 nBytesRead,这对于非类类型意味着您正在对其进行零初始化。零初始化的意思正是您所想的意思。


您所做的称为<​​em>列表复制初始化。来自 [dcl.init]:

The initialization that occurs in the = form of a brace-or-equal-initializer or [...] is called copy-initialization.

来自 [dcl.init.list]:

List-initialization is initialization of an object or reference from a braced-init-list. Such an initializer is called an initializer list, and the comma-separated initializer-clauses of the list are called the elements of the initializer list. An initializer list may be empty. List-initialization can occur in direct-initialization or copy-initialization contexts; list-initialization in a direct-initialization context is called direct-list-initialization and list-initialization in a copy-initialization context is called copy-list-initialization.

地点:

List-initialization of an object or reference of type T is defined as follows:
— If T is a class type and [...]
— Otherwise, if T is a character array and [...]
— Otherwise, if T is an aggregate, [...]
— Otherwise, if the initializer list has no elements and T is a class type [...]
— Otherwise, if T is a specialization of std::initializer_list, [...]
— Otherwise, if T is a class type, [...]
— Otherwise, if the initializer list has a single element [...]
— Otherwise, if T is a reference type, [...]
— Otherwise, if the initializer list has no elements, the object is value-initialized.

值初始化,对于非类类型,意味着[dcl.init]:

To value-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type with either no default constructor [...]
— if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor [...]
— if T is an array type, [...]
otherwise, the object is zero-initialized.

零初始化意味着,[dcl.init]:

To zero-initialize an object or reference of type T means:
— if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T

关于c++ - 大括号初始化没有任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32648805/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com