Is 'using namespace std;' valid without 'namespace std {};' first?

Is 'using namespace std;' valid without 'namespace std {};' first?

Post by Petter Rei » Wed, 24 Nov 2004 23:41:39

I ran into a problem on HP-UX 11.00 the other day, where it refused to
compile a program using 'using namespace std;' at the top. The reason
seem to be that the compiler refuses to accept 'using namespace std;'
unless the std namespace was declared first.

This triggered my curiosity, and I tried to find out what the ANSI C++
standard had to say about this. I'm unable to find a conclusion, and
hope someone here have a clue to spare.

Is this code legal ANSI C++, if it is in a file on its own?

using namespace std;

It compiles when using GCC, but now when using "aCC: HP aC++/ANSI C
B3910B A.05.50 [May 15 2003]". The HP-UX compiler protests with "Only
namespace names are valid here."

This code compiles without problems, so I concluded that it was the
lack of a namespace declaration that triggered the bug:

namespace std {};
using namespace std;

Which compiler got this one right? GCC or HP-UX aC++?

Is 'using namespace std;' valid without 'namespace std {};' first?

Post by Rob Willis » Thu, 25 Nov 2004 00:13:50


7.3 Declarations / Namespaces.

Its possible that gcc (g++) is automagicly including a file that
declares namespace std, if so it isn't compiling in Standard conforming
mode or it has a bug.

Thats correct see 7.3.4.

The latter.

BTW don't expect the standard to say the likes off:

" ... the identifier *must* be an existing namespace-name ... "

as it doesen't need too, as it is specified in the grammar.



Is 'using namespace std;' valid without 'namespace std {};' first?

Post by Howar » Thu, 25 Nov 2004 01:26:24

You need to have std declared somewhere before you can use it. But adding
"namespace std {};" isn't really the answer. If you need to use anything
from the std namespace, you should first include the header file that
declares those items in that you need. After that include point, you'll be
able to safely put your using statement without getting an error.

For example.

#include <iostream>
using namespace std;

However, you might reconsider using the whole namespace. Why not just the
items you need, like this:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

That's going to reduce the amount of *** from the std namespace that you
actually bring into your compiled executable. No sense bringing in
everything in the namespace if you only need a few things, eh?


Is 'using namespace std;' valid without 'namespace std {};' first?

Post by oldwol » Thu, 25 Nov 2004 06:59:13

using namespace ::(opt) nested-name-specifier(opt) namespace-name ;




and "std" fits the defintion of "identifier", so it seems to be
valid according to the grammar.

AFAICS the relevant section is 3.4.6:
When looking up a /namespace-name/ in a /using-directive/
or /namespace-alias-definition/, only namespace names are

If 'std' is not a namespace name at the point of lookup (which
I presume is the point of the using directive), then the lookup
must fail (which I again presume means that there should be a
compiler error).

Is 'using namespace std;' valid without 'namespace std {};' first?

Post by Rob Willis » Thu, 25 Nov 2004 09:51:47


My apologies, I gave the impression that *all* you had to do
was read the grammar, which is clearly incorrect.

After reading 7.3.1/1 (the grammar) follow it with /2:

The identifier in an original-namespace-definition shall not have
been previously defined in the declarative region in which the
original-namespace-definition appears. The identifier in an
original-namespace definition is the name of the namespace.
Subsequently in that declarative region, it is treated as an

So an identifier *isn't* an original-namespace-name until
it has appeared in an original-namespace-definition.

The point I was trying (failing) to make was that you need to
read the grammar, you can't just rely on the normal text.

The grammar connects namespace-name and original-namespace-name.
Further reading connects original-namespace-definition and the
proscription that an original-namespace-name only exists *after*
the original-namespace-defenition.

That is saying other names are not considered:

#include <iostream>
namespace A
namespace B { int x; }

class B {};
int B;

using namespace A;
using namespace B;

int main()
x = 10;
std::cout << x << " Ok\n";

(* Note: gcc (g++) doesn't get this right *)


Is 'using namespace std;' valid without 'namespace std {};' first?

Post by Petter Rei » Thu, 25 Nov 2004 17:43:51


The problem at hand here, is how to write code in a way that work on
both ANSI C++ compliant compilers, and older slightly broken
compilers. On HP-UX 11.00, the compiler seem to understand
namespaces, but the header files do not use the std namespace. So it
does not help to include the system headers. On all the other
archs/compilers, the header files declare the std namespace, but the
code need to compile in both situations.

Your examples assume only confirming implementations, while I need to
find a ANSI C++ compliant way to write the code which work on broken
and correct architectures.

(If GNU C++ is wrong in this regard, it should be changed to follow
the standard, but it will take years before I see the effect of such
change. :)

Is 'using namespace std;' valid without 'namespace std {};' first?

Post by Howar » Fri, 26 Nov 2004 02:01:25

I'm confused. How can the compiler "understand" namespaces if such
namespaces are not declared anywhere? There must be some setting for the
compiler or some included file somewhere that causes the std namespace to be
included. Or maybe there's a pre-compiled header being used.

What happens if you do something like this:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


If that fails on the std:: namespace qualifier, but the include of
<iostream> works (no warnings or errors), then I'd suspect your system path
settings or compler options. Maybe you're using the wrong system files or
something. (I'm not familiar with either compiler, so I can't suggest

In any case, you should always use the correct method, which is to include
the required headers before using any namespace(s) from them. If the
correct method fails, then ask in a newsgroup devoted to the failing
compiler, or ask the vendor. Or, don't use the broken compiler.


Is 'using namespace std;' valid without 'namespace std {};' first?

Post by Petter Rei » Sat, 27 Nov 2004 00:32:21


hp-ux 11.00# aCC -c x.c
Error 112: "x.c", line 1 # Include file <iostream> not found.
#include <iostream>
Error 19: "x.c", line 2 # Unexpected 'std'.
using std::cout;
Error 484: "x.c", line 2 # Global object '::cout' not found.
using std::cout;
Error 19: "x.c", line 3 # Unexpected 'std'.
using std::cin;
Error 484: "x.c", line 3 # Global object '::cin' not found.
using std::cin;
Error 19: "x.c", line 4 # Unexpected 'std'.
using std::endl;
Error 484: "x.c", line 4 # Global object '::endl' not found.
using std::endl;
hp-ux 11.00#

I'm pretty sure that the problem is that the headers lack the std
namespace specifier by default. Recently I've been told that adding
-AA helps, and it does.

When trying to write code compilable on both broken and conforming
compilers, I need to stay away from some features, and use some
features in a special way. The hard part is to find out which
features to stay away from, and which to use in a special way. :)

Thank you all of you for explaining why aCC got it right, and GNU C++
got it wrong, and giving me a few clues on how to write my code to
make it portable across all the compiler/arch combinations I need to
use. :)

Is 'using namespace std;' valid without 'namespace std {};' first?

Post by oldwol » Sat, 27 Nov 2004 05:29:20

Right, I'm with you now. I guess this is what they mean when
they say non-context-free grammar.

Is 'using namespace std;' valid without 'namespace std {};' first?

Post by Howar » Wed, 01 Dec 2004 00:28:37

In this case, the problem is that the header file is never getting included.
(Thus, none of those symbols, including std, are defined.) That could be
due to an old implementation, wrong compiler switches, or missing path. You
could try #include <iostream.h> and see if that works.

Ok, that's good. I'm not at all familiar with those complers ot their
switches, personally.