第5章 技巧性基础:5.2 零初始化

5.2 Zero Initialization

5.2 零初始化

 

For fundamental types such as int, double, or pointer types, there is no default constructor that initializes them with a useful default value. Instead, any noninitialized local variable has an undefined value:

对于基本类型(如int、double或指针类型),他们没有默认构造函数用于初始化一个有用的默认值。相反,任何未初始化的局部变量都有一个未定义的值:

void foo()
{
    int x; // x是一个未定义的值
    int* ptr; // ptr指向任何位置 (而不是没有指向任何地方)
}

Now if you write templates and want to have variables of a template type initialized by a default value, you have the problem that a simple definition doesn’t do this for built-in types:

现在,如果你想编写模板并希望使用默认值初始化模板类型的变量,那么你会遇到以下的问题:对于内置类型,简单定义无法做到这一点:

template<typename T>
void foo()
{
    T x; // x 如果T是内置类型,则x是一个未定义的值
}

For this reason, it is possible to call explicitly a default constructor for built-in types that initializes them with zero (or false for bool or nullptr for pointers). As a consequence, you can ensure proper initialization even for built-in types by writing the following:

由于这个原因,可以为内置类型显式调用默认构造函数,该构造函数将其初始化为零(对于bool则为false、对于指针则为nullptr)。因此,即使对于内置类型,也可以通过编写如下代码来确保正确的初始化:

template<typename T>
void foo()
{
    T x{}; // 如果T是内置类型,x 为0(或false)
}

This way of initialization is called value initialization, which means to either call a provided constructor or zero initialize an object. This even works if the constructor is explicit.

这种初始化方法称为“值初始化”,这意味着要么调用构造函数要么使用零初始化对象。甚至构造函数被声明为explicit时也可以这样做:

 

Before C++11, the syntax to ensure proper initialization was

在C++11之前,确保正确初始化的语法是

T x = T(); //如果T是内置类型,x 为0(或false)

 

Prior to C++17, this mechanism (which is still supported) only worked if the constructor selected for the copy-initialization is not explicit. In C++17, mandatory copy elision avoids that limitation and either syntax can work, but the braced initialized notation can use an initializer-list constructor if no default constructor is available.

在C++17之前,这个机制(仍然被支持)仅在复制初始化(使用“=”)为非explicit时构造函数才会被选择。在C++17中,强制的“复制省略”策略避免了这种限制,并且任何一种语法都可以使用。但是如果没有默认构造函数,则大括号初始化符号可以调用initialize_list构造函数。

To ensure that a member of a class template, for which the type is parameterized, gets initialized, you can define a default constructor that uses a braced initializer to initialize the member:

为了确保类模板成员(该成员己被参数化)获得初始值,你可以使用定义一个构造函数,该函数使用大括号初始化器来初始化成员。

template<typename T>
class MyClass {
private:
    T x;
public:
    MyClass() : x{} { //确保对于内置类型 x也可以被初始化
    }
    …
};

The pre-C++11 syntax

C++11之前的语法

MyClass() : x() { //确保对于内置类型 x也可以被初始化
}

also still works.

也可以工作。

 

Since C++11, you can also provide a default initialization for a nonstatic member, so that the following is also possible:

从C++11开始,你也可以为非静态成员提供一个默认初始值,因此下面的代码也是可以的:

template<typename T>
class MyClass {
private:
    T x{}; //零初始化x (除非另外指定)
    …
};

However, note that default arguments cannot use that syntax. For example,

但是,请注意默认参数值不能使用该语法。例如,

template<typename T>
void foo(T p{}) { //ERROR
    …
}

Instead, we have to write:

相反,我们必须这样写

template<typename T>
void foo(T p = T{}) { //OK (在C++11之前需要使用 T())
    …
}

 

原文链接: https://www.cnblogs.com/5iedu/p/12731324.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    第5章 技巧性基础:5.2 零初始化

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/392611

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月3日 下午2:53
下一篇 2023年4月3日 下午2:53

相关推荐