Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "using namespace" question

Reply
Thread Tools

"using namespace" question

 
 
cppaddict
Guest
Posts: n/a
 
      04-23-2004
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;

int main( ) {

int not_std_namespace = 1;
cout << "Hello World" << endl;
cout << not_std_namespace << endl;
return (0);
}


How does C++ know that the variable not_std_namespace is not in the
std:: namespace? Or is it automatically put there? That is, because
of the "using namespace" directive at the top of the file, references
to "cout" are treated as "std::cout." So why isn't
"not_std_namespace" treated as "std::not_std_namespace"? Or does C++
first search the local namespace, and then search the std:: namespace?

Thanks for any clarification,
cpp
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      04-23-2004
"cppaddict" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> 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;
>
> int main( ) {
>
> int not_std_namespace = 1;
> cout << "Hello World" << endl;
> cout << not_std_namespace << endl;
> return (0);
> }
>
>
> How does C++ know that the variable not_std_namespace is not in the
> std:: namespace? Or is it automatically put there? That is, because
> of the "using namespace" directive at the top of the file, references
> to "cout" are treated as "std::cout." So why isn't
> "not_std_namespace" treated as "std::not_std_namespace"? Or does C++
> first search the local namespace, and then search the std:: namespace?
>
> Thanks for any clarification,
> cpp


using namespace std;

does not put you into namespace std. Instead it imports the names from
namespace std into the current namespace, which is the global namespace in
this example. To get into namespace std, you would call

namespace std
{
//stuff
}

In your example, the following puts not_std_namespace into namespace std:

#include <iostream>
namespace std
{
int not_std_namespace = 1;
}
using namespace std;
int main()
{
cout << "Hello World" << endl;
cout << not_std_namespace << endl;
// NOTE that the next line now compiles
cout << std::not_std_namespace << endl;
return 0;
}


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

 
Reply With Quote
 
 
 
 
cppaddict
Guest
Posts: n/a
 
      04-23-2004

>does not put you into namespace std. Instead it imports the names from
>namespace std into the current namespace, which is the global namespace in
>this example.


Ahh.... That is what I wanted to know. Thanks very much.

cpp
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      04-23-2004
Just don't put:

using namespace std


in a header file. The Header file gets parsed into the Source-code
file that includes it, and thus, without the source-code man knowing,
his entire Source-code file has brought in the stuff from std!

So, I'd suggest, in the header files:

void Hello(void)
{
std::cout << "Hello!!";
}


-JKop
 
Reply With Quote
 
Michiel Salters
Guest
Posts: n/a
 
      04-23-2004
cppaddict <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> 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;
>
> int main( ) {
>
> int not_std_namespace = 1;
> cout << "Hello World" << endl;
> cout << not_std_namespace << endl;
> return (0);
> }


It's common, especially in small programs. As long as it's not used
in headers, risks are pretty low.

> How does C++ know that the variable not_std_namespace is not in the
> std:: namespace? Or is it automatically put there? That is, because
> of the "using namespace" directive at the top of the file, references
> to "cout" are treated as "std::cout." So why isn't
> "not_std_namespace" treated as "std::not_std_namespace"? Or does C++
> first search the local namespace, and then search the std:: namespace?


It's more the other way around. After the using directive, the compiler
adds 'links' to all std:: members in the global namespace. References
to cout resolve to this 'link', and the compiler follows it to get
std::cout. The entry for 'not_std_namespace' is a real entry.

The compiler must add links, because it needs to know that cout is
actually in std::. The lookup for operator<< must find
std:perator<< . The compiler knows it has to look in std::
because of Argument Dependent Lookup (=Koenig lookup). Unqualified
names are looked up also in the namespaces of their arguments.

Regards,
Michiel Salters
 
Reply With Quote
 
fabio de francesco
Guest
Posts: n/a
 
      04-23-2004
cppaddict <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
>
> How does C++ know that the variable not_std_namespace is not in the
> std:: namespace? Or is it automatically put there? That is, because
> of the "using namespace" directive at the top of the file, references
> to "cout" are treated as "std::cout." So why isn't
> "not_std_namespace" treated as "std::not_std_namespace"? Or does C++
> first search the local namespace, and then search the std:: namespace?
>

All C++ symbols are defined in a larger context called namespace.
This is used to avoid name conflicts when a programmer wold define a
function that has the same name in the C++ library.
When a programmer writes 'using namespace std' is saying to the
compiler to use all the symbols it finds in a program and that match
with one in std namespace.
Obviously it doesn't find any matching symbol for
'my_no_std_function()' in the std namespace and so it looks for
declarations and definitions in your program and your files included.
When you want to define your own symbols that must be used instead of
the ones in std namespace you must do the following:

// in myheader.h
namespace MyNameSpace
{
int funct()
{
...
}
}

// in main.cpp
#include "myheader.h"
....
using namespace std;
....
int main()
{
using MyNameSpace::funct; // 'funct' without parenthesis
...
funct(); // It uses MyNameSpace::funct()
std::funct(); // It uses std::funct()
::funct(); // It's the same as before: std::funct()
}

Ciao, Fabio De Francesco.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments