gpt4 book ai didi

c++ - 为什么 函数不需要 "using std::xxx"?

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:22 25 4
gpt4 key购买 nike

对于您可以包含的所有其他标准库 header ,有必要通过以下任一方法指定命名空间:

using namespace std;
using std::xxx;
int main() {
std::xxx;
}

到目前为止我遇到的唯一异常是在 <cmath> 中库,当我到目前为止使用的所有功能都不需要上述任何功能即可在不指定 namespace 的情况下使用它们。这是为什么?

注意:我可能错了 <cmath>是唯一不需要指定命名空间的标准库头,或者 <cmath> 中的每个函数表现得像这样。我只是在日常使用中还没有遇到过我所说的异常(exception)情况。

最佳答案

C++ 标准允许实现将在标准 C header 中声明的名称放在全局命名空间中。

来自 C++ 标准(17.6.1.2 header )

  1. ...In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).

关于c++ - 为什么 <cmath> 函数不需要 "using std::xxx"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34240531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com