zoukankan      html  css  js  c++  java
  • 第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())
        …
    }
  • 相关阅读:
    ubuntu 9.04更新源
    想学一下asp.net,跟着书本做了个bbs
    [转]ubuntu系统中遇到的一些问题及解决
    第一篇,打个招呼
    人际交往的书籍推荐
    程序员的五层境界,你在哪一层?
    HTTP报文之"请求报文"和"响应报文"详解
    如何提高你的工作效率?
    面对焦虑我们怎么办 ?
    CEO要看的书籍推荐
  • 原文地址:https://www.cnblogs.com/5iedu/p/12731324.html
Copyright © 2011-2022 走看看