c++ - "using namespace std;"在 "#include <foo>"之前

标签 c++ c++11

以下两个源文件在可编译性或生成的代码(如果有的话)方面有何不同:

附件 A:

namespace std {};
using namespace std;
#include <vector>
#include <string>

<any code here>

图表 B:

#include <vector>
#include <string>
using namespace std;

<any code here>

假设两个<any code here>占位符被替换为任何相同的用户代码。

换句话说:如果“使用命名空间标准;”,是否有任何用户可见的差异?放在标准之前 #includes (假设命名空间std已经如上介绍)?

最佳答案

以下代码可能不太可能出现在您的实现的 vector header 中:

namespace __AA
{
    class vector {};
}

namespace std
{
    // actual std::vector implementation here
}

namespace __BB
{
    using namespace __AA;
    vector x;
}

现在有了Exhibit A,你就有点模棱两可了。

关于c++ - "using namespace std;"在 "#include <foo>"之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9952521/

相关文章:

c++ - 不想第一次检查条件?

c# - 如何在 C# 中编码集合以传递给 native (C++) 代码

c++ - g++ 中的模板规范编译错误

c++ - 在一个函数中使用 namespace 中的符号,但在同一文件中不使用另一个 namespace 中的符号

c++ - 如何更改(向前/向后移动)基于范围的 for 循环中的迭代器?

c++ - 比较不同单词在输入 C++ 中出现的次数

C++ 理解迭代器和删除

c++ - 非原子对象在所有线程中是否具有相同的修改顺序? (在没有数据竞争的情况下)

c++ - std::map-like 类型,键上没有比较/哈希

c++ - 有什么方法可以编译时检查字符串用户定义的文字吗?