简体   繁体   English

字符串和字符串数组的零初始化(C++)

[英]Zero initialization of string and string array (C++)

According to https://en.cppreference.com/w/cpp/language/zero_initialization根据https://en.cppreference.com/w/cpp/language/zero_initialization

在此处输入图片说明

In the example provided by the documentation:在文档提供的示例中:

std::string s; // is first zero-initialized to indeterminate value
               // then default-initialized to ""

Why does zero initialization occur to string s;为什么string s;会发生零初始化string s; if the syntax is for static T object;如果语法是针对static T object; ? ?

Why does zero initialization happen before default initialization and why are both allowed to happen?为什么零初始化发生在默认初始化之前,为什么两者都允许发生?

The effects of zero initialization are:零初始化的效果是:

  • If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.如果 T 是标量类型,则对象的初始值是显式转换为 T 的整数常量零。
  • If T is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits.如果 T 是非联合类类型,则所有基类和非静态数据成员都初始化为零,并且所有填充都初始化为零位。 The constructors, if any, are ignored.构造函数(如果有)将被忽略。
  • If T is a union type, the first non-static named data member is zero-initialized and all padding is initialized to zero bits.如果 T 是联合类型,则第一个非静态命名数据成员被零初始化,所有填充都被初始化为零位。
  • If T is array type, each element is zero-initialized如果 T 是数组类型,则每个元素都初始化为零
  • If T is reference type, nothing is done.如果 T 是引用类型,则什么都不做。

What if I initialize string array[2] = {"Test1"};如果我初始化string array[2] = {"Test1"}; ? ? I know that the array will contain "Test1" and empty string "".我知道数组将包含“Test1”和空字符串“”。

But according to the above documentation,但是根据上面的文档,

If T is array type, each element is zero-initialized如果 T 是数组类型,则每个元素都初始化为零

The data type is string which is an object / reference type?数据类型是字符串,它是对象/引用类型吗?

If T is reference type, nothing is done.如果 T 是引用类型,则什么都不做。

Nothing is done?什么都没做? I thought maybe a constructor would have been called.我想也许会调用一个构造函数。 Surely an empty string is something?空字符串肯定是什么吗?

Why does zero initialization occur to string s;为什么string s;会发生零初始化string s; if the syntax is for static T object;如果语法是针对static T object; ? ?

Why does zero initialization happen before default initialization and why are both allowed to happen?为什么零初始化发生在默认初始化之前,为什么两者都允许发生?

In page you linked to, that defines a non-local variable.在您链接到的页面中,它定义了一个非局部变量。

Non-local variables are initialized in two phases.非局部变量分两个阶段初始化。

  1. Static intialization.静态初始化。
  2. Dynamic initialization, if it applies.动态初始化(如果适用)。

In static initialization phase, a variable is initialized using constant initialization or zero initialization静态初始化阶段,使用常量初始化零初始化初始化变量

Dyanmic initialization is used, if it applies, such as for objects that have the appropriate constructor or for objects that are initialized using an expression that can be evaulated at run time.使用动态初始化(如果适用),例如用于具有适当构造函数的对象或使用可在运行时评估的表达式初始化的对象。

You can read more on the topic at https://en.cppreference.com .您可以在https://en.cppreference.com上阅读有关该主题的更多信息。

Nothing is done?什么都没做? I thought maybe a constructor would have been called.我想也许会调用一个构造函数。 Surely an empty string is something?空字符串肯定是什么吗?

A reference cannot be zero-initialized.引用不能被零初始化。 It can only be initialized using a object that it will be a reference to.它只能使用将作为引用的对象进行初始化。

(Unless otherwise specified, all declarations in this answer are assumed to be in namespace scope.) (除非另有说明,否则假定此答案中的所有声明都在命名空间范围内。)

Why does zero initialization occur to string s;为什么string s;会发生零初始化string s; if the syntax is for static T object;如果语法是针对static T object; ? ?
Why does zero initialization happen before default initialization and why are both allowed to happen?为什么零初始化发生在默认初始化之前,为什么两者都允许发生?

Variables with static storage duration are first zero-initialized at compile time, and then optionally dynamically initialized at runtime.具有静态存储持续时间的变量首先在编译时初始化为零,然后在运行时可选地动态初始化。 static T object; declares an object of static storage duration.声明一个静态存储持续时间的对象。 For a simple declaration like对于一个简单的声明,如

int x;

The dynamic initialization is not performed.不执行动态初始化。 For a more sophisticated declaration like对于更复杂的声明,如

std::string s;

Zero-initializing a string may result in an invalid string with a broken class invariant.对字符串进行零初始化可能会导致无效的字符串具有破坏的类不变量。 Therefore, the dynamic initialization calls the default constructor to ensure that the object is valid.因此,动态初始化调用默认构造函数来确保对象有效。

What if I initialize string array[2] = {"Test1"};如果我初始化string array[2] = {"Test1"}; ? ? I know that the array will contain "Test1" and empty string "".我知道数组将包含“Test1”和空字符串“”。

First, at compile time, the two objects are zero-initialized, resulting in possible invalid state.首先,在编译时,这两个对象被零初始化,导致可能的无效状态。 Then, at runtime, the constructors are called ( const char* constructor for the first object and default constructor for the second object), and the valid objects are constructed.然后,在运行时,调用构造函数(第一个对象的const char*构造函数和第二个对象的默认构造函数),并构造有效的对象。

The data type is string which is an object / reference type?数据类型是string ,它是对象/引用类型吗?

std::string is an object type instead of a reference type. std::string是对象类型而不是引用类型。

[For a reference type] Nothing is done? 【供参考型】什么都没做? I thought maybe a constructor would have been called.我想也许会调用一个构造函数。 Surely an empty string is something?空字符串肯定是什么吗?

A reference type is not considered an actual "object", so there is no point in specifying its zero-initialization semantics.引用类型不被视为实际的“对象”,因此指定其零初始化语义没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM