473,396 Members | 1,827 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,396 software developers and data experts.

"using namespace" question

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
5 1915
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
4
by: Teddy | last post by:
If I use just a STL container such as std::vector in my program. Is "using std::vector;" better than "using namespace std" ? Does "using namespace std" cost ?
5
by: SenthilSS | last post by:
My application produces XML Data files which have XML namespace qualified XML elements (nodes), but the namespace itself is not declared in the data file. My task is to read these data files in a...
3
by: Charles Stanley | last post by:
Hi all, I'm new to C#, and have two pretty simple questions: I have a class, in a .cs file by itself. I need to make use of the sole function in this class in another .cs file. Here's the...
3
by: ARZ | last post by:
Hi How do I include DLLs based on a condition. Eg., there are 2 DLLs 'Oracle.dll' and 'SQL.dll'. I need to include ANYONE of the DLLs based on a value. Is it possile in C# Thanks in advance ARZ
3
by: Chris | last post by:
Hi, 1) In file test.aspx, i put: <%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %> <%@ import namespace="System.Data"%> <%@ import...
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" <<...
12
by: Steve Pope | last post by:
Compiling the following works on my system: file main.cpp: #include <iostream> namespace space { int foo; }
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.