C++ LLVM 编译器 2.0:警告“使用命名空间 std;”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3952304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LLVM Compiler 2.0: Warning with "using namespace std;"
提问by Justin Mrkva
In Xcode using LLVM 2.0, when I put the line using namespace std;
in my C++ code, I get this warning:
在使用 LLVM 2.0 的 Xcode 中,当我将该行using namespace std;
放入 C++ 代码时,我收到以下警告:
Semantic Issue
Using directive refers to implicitly-defined namespace 'std'
语义问题
Using 指令指的是隐式定义的命名空间“std”
Is there a way to fix this? Why is it giving that warning?
有没有办法来解决这个问题?为什么会发出这样的警告?
回答by Motti
Have you included any standard header files? Otherwise the compiler doesn't know about namespace std
.
您是否包含任何标准头文件?否则编译器不知道namespace std
.
Please post more code to clarify.
请发布更多代码以澄清。
回答by jtony
Moving the using namespace std to after the #include can eliminate this warning.
将 using 命名空间 std 移到 #include 之后可以消除此警告。
回答by balagurubaran
i solved this problem like this
我这样解决了这个问题
#include <iostream>
using namespace std;
/// This function is used to ensure that a floating point number is
/// not a NaN or infinity.
inline bool b2IsValid(float32 x)
{
if (x != x)
{
// NaN.
return false;
}
float32 infinity = std::numeric_limits <float32>::infinity();
return -infinity < x && x < infinity;
return true;
}
回答by GC Saab
I see that this question is pretty old, but for anyone checking this out in the future, I wanted to add this link from the LLVM documentation as a supplement to the discussion and for poeple looking for more info:
我看到这个问题已经很老了,但是对于将来检查此问题的任何人,我想添加 LLVM 文档中的此链接作为讨论的补充,并供寻求更多信息的人使用:
LLVM Coding Standards: Do Not use using namespace std;
LLVM 编码标准:不要使用 using namespace std;
I believe that the title is pretty indicative of why I've shared it to help with this question.
我相信这个标题很好地说明了为什么我分享它来帮助解决这个问题。
In LLVM, we prefer to explicitly prefix all identifiers from the standard namespace with an “std::” prefix, rather than rely on “using namespace std;”.
In header files, adding a 'using namespace XXX' directive pollutes the namespace of any source file that #includes the header. This is clearly a bad thing.
在 LLVM 中,我们更喜欢用“std::”前缀显式地为标准命名空间中的所有标识符添加前缀,而不是依赖“使用命名空间 std;”。
在头文件中,添加 'using namespace XXX' 指令会污染 #include 头文件的任何源文件的命名空间。这显然是一件坏事。
Edit: So instead if using 'using std namespace;' explicitly type std:: for every case where you with to use the standard library. It avoids conflicts with source file namespaces. This is what the quote above from the article advises.
编辑:所以如果使用'使用 std 命名空间;' 对于要使用标准库的每种情况,显式键入 std::。它避免了与源文件命名空间的冲突。这就是文章上面引用的建议。