开发者

LLVM Compiler 2.0: Warning with "using namespace std;"

In Xcode using LLVM 2.0, when I put the line using namespace std; in my C++ code, I get this warning:

Semantic Issue

Using directive refers to implicitly-defined namespace 'std'

Is开发者_开发技巧 there a way to fix this? Why is it giving that warning?


Have you included any standard header files? Otherwise the compiler doesn't know about namespace std.

Please post more code to clarify.


Moving the using namespace std to after the #include can eliminate this warning.


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;

}


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 Coding Standards: Do Not use 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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜