473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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


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++?

Jul 22 '05 #1
8 4880
Petter Reinholdtsen wrote in news:2f*************@saruman.uio.no in
comp.lang.c++:

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.
7.3 Declarations / Namespaces.

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

using namespace std;

It compiles when using GCC,
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.
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."
Thats correct see 7.3.4.

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++?


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.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

"Petter Reinholdtsen" <pe**@hungry.com> wrote in message
news:2f*************@saruman.uio.no...

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++?


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 crap 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?

-Howard

Jul 22 '05 #3
Rob Williscroft <rt*@freenet.co.uk> wrote:
Petter Reinholdtsen wrote:
Is this code legal ANSI C++, if it is in a file on its own?

using namespace std;


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.


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

namespace-name:
original-namespace-name
namespace-alias

namespace-alias:
identifier

original-namespace-name:
identifier

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
considered.

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).
Jul 22 '05 #4
Old Wolf wrote in news:84**************************@posting.google.c om in
comp.lang.c++:
Rob Williscroft <rt*@freenet.co.uk> wrote:
Petter Reinholdtsen wrote:
> Is this code legal ANSI C++, if it is in a file on its own?
>
> using namespace std;
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.


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

namespace-name:
original-namespace-name
namespace-alias

namespace-alias:
identifier

original-namespace-name:
identifier

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


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
original-namespace-name.

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.
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
considered.
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 *)

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).


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

"Petter Reinholdtsen" <pe**@hungry.com> wrote in message
news:2f*************@saruman.uio.no...

[Howard]
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.


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. :)


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
specifics.)

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.

-Howard


Jul 22 '05 #6

[Howard]
What happens if you do something like this:

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

?
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.
Or, don't use the broken compiler.


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. :)
Jul 22 '05 #7
Rob Williscroft <rt*@freenet.co.uk> wrote:
Old Wolf wrote:
Petter Reinholdtsen wrote:

> Is this code legal ANSI C++, if it is in a file on its own?
> using namespace std;


original-namespace-name:
identifier

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


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
original-namespace-name.

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


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

"Petter Reinholdtsen" <pe**@hungry.com> wrote in message
news:2f*************@saruman.uio.no...

[Howard]
What happens if you do something like this:

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

?
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.


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.
Recently I've been told that adding
-AA helps, and it does.


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

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: cppaddict | last post by:
It is typical to put the line: using namespace std; at the top of a file which makes use of std library objects. To take a simple example: #include <iostream> using namespace std;
2
by: Jacek Dziedzic | last post by:
Is it valid to use a "using namespace foo" (as opposed to using foo::bar which I'm sure is legal) within a class declaration? My compiler rejects it, but I've been told it's valid. Can anyone...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
9
by: Animatorboy | last post by:
Ok I am making a very simple program (I am new to this so I dont know much, its kinda a trial and error thing) This is what I am trying to do: -Get a name from the user (designated as variable...
6
by: Halcyon | last post by:
so i've been "using namespace std" happily in all my source files at the global scope, and i then go to to use cout, vector, string etc without having to use std:: everytime. However today i was...
3
by: Schizoid Man | last post by:
Hi, I'm a novice whose just about migrating to C++ after cutting my teeth on C for a few years. To start with I'm using Microsoft's out-of-the-box Visual C++ Express Edition compiler and I...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
6
by: Juha Nieminen | last post by:
Whenever one sees example C++ code basically anywhere, be it in a book, in a tutorial in the internet, in an online forum or whatever, I would estimate that at least in 99% of cases one sees the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.