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

And*_*zos 0 c++ c++11

以下两个源文件之间的可编译性或生成的代码(如果有)有何不同之处:

图表A:

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

<any code here>
Run Code Online (Sandbox Code Playgroud)

图表B:

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

<any code here>
Run Code Online (Sandbox Code Playgroud)

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

换句话说:如果"using namespace std;"有任何用户可见的区别吗?放在标准之前#includes(假设如上所述引入了命名空间std)?

Ben*_*ley 5

尽管不太可能,但以下代码可能在您的实现的向量头中:

namespace __AA
{
    class vector {};
}

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

namespace __BB
{
    using namespace __AA;
    vector x;
}
Run Code Online (Sandbox Code Playgroud)

现在有了附录A,你就有了含糊之处.