473,695 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4909
Petter Reinholdtsen wrote in news:2f******** *****@saruman.u io.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.co m> wrote in message
news:2f******** *****@saruman.u io.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.goo gle.com 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.co m> wrote in message
news:2f******** *****@saruman.u io.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.co m> wrote in message
news:2f******** *****@saruman.u io.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
1949
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
6397
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 please confirm or deny? TIA, - J.
5
5708
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. ================================================================= Error 19: "CORBAManagerMessages.h", line 4 # Unexpected 'std'. using std::string; ^^^
30
4108
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" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
9
1629
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 'name') -Take that name and say this to them "Well, Helllo 'name' welcome. Then from there I dont know what I wanna do, but this is what I have
6
9008
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 informed that i should eschew "using namespace std" and use "using std::string", "std::vector" et al. I don't quite get what the potential drawbacks of "using namespace std" are... i perused a few books, but most authors *do* use "using...
3
2221
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 had a couple of questions: 1. If I am using namespace std, is there a specific reason for me to place std:: before all the manipulators - cout, endl, etc? I've noticed
11
1913
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 destruction of the assigned memory seems to call the class destructor more than once. I don't know the reason or whether I used the vector class correctly. Attached is my program. Thanks for your help. Regards,
6
2247
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 use of "using namespace std;" to get rid of that namespace. In fact, "using namespace ..." is very popular with all documentation and example code of most C++ libraries out there which use their own namespace. This raises the question why use...
0
8628
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8567
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9114
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5839
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3001
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2271
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1973
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.