"using namespace" question

C

cppaddict

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
 
J

John Carson

cppaddict said:
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;
}
 
C

cppaddict

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
 
J

JKop

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
 
M

Michiel Salters

cppaddict said:
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::eek: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
 
F

fabio de francesco

cppaddict said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top