简体   繁体   English

我可以在C ++标准中找到在全局范围内调用函数的支持吗?

[英]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.

相关问题 在c ++中,仅在全局范围内允许使用“表达式”来初始化全局对象。 在标准中哪里可以找到? - In c++, `expressions` are allowed in global scope only to initialize global objects. Where can I find this in the Standard? 在哪里可以找到标准的 Visual C++ 头文件? - Where can I find the standard Visual C++ header files? 在哪里可以找到当前的 C 或 C++ 标准文档? - Where do I find the current C or C++ standard documents? C++ 在哪里可以找到 gcc 编译器标准库的实现文件? - C++ where I can find the implementation files of the standard library for the gcc compiler? C ++标准库与凡人制作代码+我在哪里可以找到来源? - C++ standard library vs mortal made code + where can I find the sources? 在哪里可以找到 C++ 语言的标准 BNF 或 YACC 语法? - Where can I find standard BNF or YACC grammar for C++ language? 我在哪里可以找到适合我的 C++ 项目的好的 Scope Guard 实现? - Where can I find a good Scope Guard implementation for my C++ projects? 我在哪里可以看到C ++标准库中使用的代码? - Where can I see the code used in C++ standard libraries? C++ 可以在全局范围内拥有代码吗? - Can C++ have code in the global scope? C ++在全局命名空间中找不到非标准C函数 - C++ can't find non-standard C functions in global namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM