How to comprehend that an implementation is permitted to treat dynamic initialization of non-local variable as static initialization in some cases?

浪子不回头ぞ 提交于 2019-12-19 09:21:39

问题


In fact, the problem comes from the words in the standard draft N4582:

[basic.start.static/3] An implementation is permitted to perform the initialization of a variable with static or thread storage duration as a static initialization even if such initialization is not required to be done statically, provided that

— the dynamic version of the initialization does not change the value of any other object of static or thread storage duration prior to its initialization, and

— the static version of the initialization produces the same value in the initialized variable as would be produced by the dynamic initialization if all variables not required to be initialized statically were initialized dynamically.

Do these words mean that if the two conditions are satisfied, a non-local variable of class type may be fully initialized statically (zero-initialized) so that its constructor is not called (since the dynamic version, initializing by calling a constructor, may be replaced by a static version)?


回答1:


Static initialization is performed during compilation/linking. The compiler/linker assigns a location to the variable in the static memory and fills it with the correct bytes (the bytes don't need to be all zeros). When the program starts, those regions of the static memory are loaded from the program's binary file and no further initialization is required.

Examples:

namespace A {
    // statically zero-initialized
    int a;
    char buf1[10];

    // non-zero initialized
    int b = 1;
    char date_format[] = "YYYY-MM-DD";
}

Unlike static initialization, dynamic initialization requires running some code after program start-up to set thus initialized variables to their initial state. The code that needs to be run doesn't need to be a constructor call.

Examples:

namespace B {
    int a = strlen(A::date_format);   (1)
    int b = ++a;                      (2)

    time_t t = time();                (3)

    struct C {
        int i;

        C() : i(123) {}
    };

    C c;                              (4)

    double s = std::sqrt(2);          (5)
}

Now, the C++ standard allows the compiler to perform the computations that would be carried out during dynamic initialization, provided that those computations do not have side effects. Besides, those computations must not depend on external environment. In the above example:

(1) can be performed statically since strlen() doesn't have any side-effects.

(2) must stay dynamic since it mutates a.

(3) must stay dynamic since it depends on external environment/makes system calls.

(4) can be performed statically.

(5) is a little tricky, since floating point computation depends on the state of the FPU (namely, rounding mode). If the compiler is told not to treat floating point arithmetic that seriously, then it can be performed statically.



来源:https://stackoverflow.com/questions/37757316/how-to-comprehend-that-an-implementation-is-permitted-to-treat-dynamic-initializ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!