20

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?

jstm88
  • 3,335
  • 4
  • 38
  • 55
  • 1
    Are you putting *just* that line? – GManNickG Oct 17 '10 at 06:51
  • 2
    Do you include any headers that have that namespace? – JoshD Oct 17 '10 at 06:51
  • 2
    Xcode 4 is still under NDA; you should ask this question in Apple's beta dev forum, it's the only forum the NDA you entered into with Apple permits you to ask. (https://devforums.apple.com/community/xcode4 -- your apple dev program login is required to read/post) – Jason Coco Oct 17 '10 at 07:33
  • 1
    GCC allows a file to have `using namespace std;` without any standard includes. This is not conforming, so clang warns you. – Johannes Schaub - litb Oct 17 '10 at 10:27
  • 1
    It does contain other includes, for example `using namespace std;` and then `#include `. The other one is `#include "sqlite3.h"`. The class is complete; it happens in any C++ header I add `using namespace std;` to. – jstm88 Oct 17 '10 at 12:23

4 Answers4

29

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

Please post more code to clarify.

Motti
  • 110,860
  • 49
  • 189
  • 262
  • 4
    Ah, I just figured it out. You're right, the line `using namespace std;` was the first line (besides the #ifndef and #define statements of course) in the file; I moved it after the line `#include ` and it eliminated the warning. GCC never gave that warning, so I never thought about it before. Thanks! – jstm88 Oct 17 '10 at 12:46
9

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

ravron
  • 11,014
  • 2
  • 39
  • 66
jtony
  • 101
  • 2
  • 4
7

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;

}
arthur.sw
  • 11,052
  • 9
  • 47
  • 104
balagurubaran
  • 79
  • 1
  • 1
-1

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.

Community
  • 1
  • 1
GC Saab
  • 91
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/13390173) – Uyghur Lives Matter Aug 19 '16 at 15:31
  • I'm at work and just trying to help other people in similar situations by providing a useful piece of information that addresses this exact issue. I think the response adds to the answers already provided. If this is problematic I will just take it down. If it was me asking this question, I would wish someone shared that statement from llvm with me. – GC Saab Aug 19 '16 at 21:50
  • 1
    @cpburnz I guess I'm new to the style that help is asked to be in because to me it would be useful. Specifically, what more could I have added to this unclear answer? It is intended as more of an alternative suggestion to answers already posted. – GC Saab Aug 19 '16 at 22:08
  • It is useful to the people who are less frequent on SO, but fuzz to those who spend considerable time interacting with these questions, hence why the vets enforce this. It's a question of cleanliness and order. – Sipty Dec 07 '16 at 14:08