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

标签 c++ namespaces c++-standard-library

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

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

到目前为止我遇到的唯一异常(exception)是 <cmath>库,当我迄今为止使用的所有函数都不需要上述任何内容时,就可以在不指定 namespace 的情况下使用它们。这是为什么?

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

最佳答案

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

来自 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/

相关文章:

c++ - 在 MacOSX 下用 C 启动特权应用程序

c++ - 对于多个进程访问的 sqlite 数据库,哪种 NFS 实现是安全的

c++ - 仅在需要时编译文件

python - 如何在 Python 中找到任何包的 "import name"?

c++ - 为 map 赋值的最有效方式

c++ - 理解c++中的库函数

c++ - 在 libcurl 连接池中预先创建连接

c++ - 将基类添加到 "New C++ Class"对话框

PHP:使用命名空间自动加载多个类

c++ - 从 const char* + length 构造 const std::string 便宜吗?