By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

"using namespace" question

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"cppaddict" <he***@hello.com> wrote in message
news:1i********************************@4ax.com
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)

Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

P: n/a
cppaddict <he***@hello.com> wrote in message news:<1i********************************@4ax.com>. ..
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::operator<< . 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
Jul 22 '05 #5

P: n/a
cppaddict <he***@hello.com> wrote in message news:<1i********************************@4ax.com>. ..

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.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.