[英]Where in the C++ Standard can I find support for invoking a function in global scope?
Consider the snippet below: 考虑下面的代码:
#include <iostream>
int f(int i) {
return ++i;
}
int i = f(i);
int main() {
std::cout << i << '\n';
}
Where in the C++ Standard can I find support for the initialization of the global variable i
above? 在C ++标准的哪里可以找到上面的全局变量
i
初始化的支持?
Initialisation of non-local variables is described in the chapter titled "Initialization of non-local variables", [basic.start.init]. 非局部变量的初始化在标题为“非局部变量的初始化”,[basic.start.init]的章节中描述。 In C++11, that's 3.6.2.
在C ++ 11中,那是3.6.2。
Initialising using =
, the initialiser can be a braced list, or any assignment expression, including a function call, as specified in [dcl.init] (C++11 8.5). 使用
=
初始化,初始化器可以是支撑列表,或任何赋值表达式,包括函数调用,如[dcl.init](C ++ 11 8.5)中所指定。
This has static storage duration, so it's first zero-initialised during static initialisation per 3.6.2/2: 这具有静态存储持续时间,因此它在静态初始化期间每3.6.2 / 2初始化为零:
Variables with static storage duration [...] shall be zero-initialized before any other initialization takes place.
具有静态存储持续时间的变量应在任何其他初始化发生之前进行零初始化。
It is then initialised from its initialiser during dynamic initialisation, since it doesn't meet the criteria for constant initialisation (since the initialiser isn't a constant expression). 然后在动态初始化期间从初始化器初始化它,因为它不满足恒定初始化的标准(因为初始化器不是常量表达式)。 That passes the statically initialised zero value to the function, which increments it and returns 1. That value of 1 is used to complete the initialisation.
它将静态初始化的零值传递给函数,函数递增它并返回1.该值1用于完成初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.