"using namespace std" Vs "std::..."

"using namespace std" Vs "std::..."

Post by scotthc » Wed, 22 Oct 2003 06:50:05


I am a C++ beginner. I have read some articles about "using namespace
std" and "std::". What is the exactly different between them? What bad
if I use "using namespace std" in my program instead of using
"std::cout","std::cin".. and so on. Thanks.

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by Tim Row » Thu, 23 Oct 2003 00:55:54


It need not be bad. "using namespace std" is the library equivalent
of getting your toolbox out of the cupboard and emptying it onto your
workbench. "std::" is the library equivalent of getting tools out of
the box as you need them and putting them away immediately.

If you use "using namespace std" then you'll get lots of identifiers
declared [1], and you'll have problems if you try to name things using
the same identifiers (which you may do accidentally, of course -- do
you know all the identifiers in namespace std? Are you /sure/ that a
future version won't include "myVar"? :-) But the "std::" approach
gets rather verbose -- do you /really/ want to keep getting that
hacksaw out again every time, when you're using it so often?

One place where /I/ think there's a clear right and wrong is in
headers. Other people are likely to use them ("other people" includes
you in six month's time!), so don't force their choice. Use "std::" ,
then anybody #including your header has the choice of whether to get
all the tools out or not, without you having forced it on them.
Beyond that it's up to you. I suppose "std::" carries less surprises,
and I tend to use it, reserving "using" for smaller namespaces, but
that's because I'm naturally timid.

[1] or an error if you haven't included anything in namespace std!

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
 
 

"using namespace std" Vs "std::..."

Post by John Downe » Thu, 23 Oct 2003 01:00:34


Well say I make my own class named string and I also have using
namespace std; then my compiler will complain because there is already a
string class from the std namespace. So I could keep my string class and
use vectors like std::vector and I wouldn't get an error.

--
John Downey
http://www.yqcomputer.com/
http://www.yqcomputer.com/
http://www.yqcomputer.com/

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by Antoun Kan » Thu, 23 Oct 2003 01:08:35


It is considered very bad manners to have a "using namespace XXX;" in
your header files, because that adds the namespace XXX into the lookup
space wherever the file is included, causing potential ambiguities (at
least while reading the code).

However, in isolated code, the impact of "using namespace" is limited,
and not much of a problem.

So, the rule is something like this:

file: foo.h
===========

// this is a file included by many, you don't want to expand
// their lookup spaces with "std" or your namespace when they
// don't want it.
#ifndef foo_dot_h
#define foo_dot_h

#include <vector>

namespace baz {
struct foo {
std::vector<int> blah;
foo();
};
}
#endif

file: foo.cpp
=============

// here, this is your "private" implementation space.
// expanding the lookup space during the compilation of
// this unit has no global impact.
#include "foo.h"
using namespace std;
using namespace baz;

foo::foo() : blah(vector<int>(10, 5)) { }


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by Jeffrey Tu » Thu, 23 Oct 2003 04:15:39


If you use "using namespace std" you are bringing a lot of extraneous
names into your program's scope. This could be a problem if you write
a function that has the same name as one of the std functions. A
valid compromise is to put "using std::cout" at the start of your
program, you only have to type the long name once and you won't
accidentally include function names that you wrote your own functions
for.

--Jeff

--
"I don't mind being characterized as a 'liberal'
- I just don't happen to believe it's true."
Howard Dean

A Dean supporter is just a Democrat who hasn't
listened to Kucinich.

http://www.yqcomputer.com/


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by Sean Frale » Thu, 23 Oct 2003 04:49:24


When you have "using namespace std" in a source file, you are telling the
compiler to declare everything that exists in the namespace std in your
current scope. If you then try to use a class or function from some other
source in you current scope that has the same name as one in namespace std,
you will get an error. This sort of defeats the purpose of namespaces.

By using "using std::", you are telling the compiler to only declare that
specific object, and will only get an error if you use another object with
that name. This allows you a bit more leeway when naming classes and
functions. Example:

using std::cin;
using std::cout;

Now you will only get an error if you use another object named cin or cout,
you can use endl, vector, min(), max(), etc. without an error.

In circumstances where you have to use two or more objects with the same
name in you current scope, then you can prepend "std::" where it is used to
tell the compiler the difference. Example:

int a = 1;
int b = 2;
int c = std::max(a, b);

int d = 3;
int e = 4;
int f = foo::max(d, e);

This will tell the compiler to use the max function from the namspace std
for the one call, and to use the max function from the namespace foo for
the other.

My advice would be the following:

1) Don't use "using namespace std" since this clutters the namespace for
you current scope, and increases the likelihood that you will need to
specify a namespace for each conflicting function call and use of a class.
This gets to be error-prone and aggravating.

2) Do use "using std::<name>", most of the time this will be sufficient.
Doing this also lets other people working with your code know which
namespace a given class comes from, which can let them know what interface
and behavior to expect.

Sean

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by sasha » Thu, 23 Oct 2003 04:55:32


The 'badness' of using namespace std; depends on what type of files you
use it in. If you use it in a header file like
...
//file.hpp
using namespace std;
...

You introduce namespace leakage where every other file that includes
file.hpp will also be aware of the contents of the std namespace. For
example
....
//file1.hpp
#include "./file.hpp"
....

Now file1.hpp also knows about std namespace even though it is not
specified explicitly.
What I do is I don't incorporate any using directives in a header file.
Instead I explicitly give the full namespace prefix of the type I'm
after. For example:
...
//file2.hpp
#include <vector>

void f(std::vector<int> v);
....

Doing this prevents namespace leakage because any file that includes
file2.hpp won't know about the any namespace.

When working with cpp files using namespace std isn't as bad because
there is no chance of leaking (unless you do something silly like
#include "file2.cpp" in another file somewhere). So an example:
....
//file2.cpp
using namespace std;
void f(vector<int> v)
{
//do something with v
}
....

Some people might prefer the following, explicitly specifying the class
used, like this:
....
//file2.cpp
using std::vector;
void f(vector<int> v)
{
//do something with v
}
....
This only brings std::vector into scope and nothing else.

In summary the rule is don't put a using directive in a header file -
fully specify the name instead.

If you are dealing with long namespaces (i.e. boost::numeric::ublas) and
don't want to type it out over and over again a namespace alias can help:
...
namespace bnu = boost::numeric::ublas
...

--
sashan
http://www.yqcomputer.com/ ~sgov008/





[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by Nils Pette » Thu, 23 Oct 2003 10:24:52


If you use "using namespace std" you have to make sure that your
function
and variable names isn't the same as any in the std namespace.
If you use std:: you can safely have a varable cout or a class string
since when you want the standard ones you'll be writing std::cout and
std::string

Using "using namespace std" results in more thinking and checking your
choice of name against those in std.
Using std:: results in more typing

You choose.

hth

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by Balog Pa » Thu, 23 Oct 2003 10:25:14


The C++ folklore says, "If you're named Scott, you may just put

using namespace std;

in yout .cpp files, provided you append a comment:

// so sue me

"

;-)

Paul





[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by Dhru » Thu, 23 Oct 2003 10:28:46


1. using namespace std will import all symbols in the standard namespace
"std" to the global namespace. So, you are in effect polluting the
global
namespace with potentially useless or un-required names.

2. Where as std::cout calls cout which resides in namespace std without
importing the whole thing to the global namespace.


Just to make things simpler, consider this:

Consider you create a swap function that does something other than
swapping
the values of it's 2 arguments:

#include <algorithm>
#include <functional>

using namespace std; //Oops, swap is now in the global namespace.

//And we are defining it again here.
template <class T>
void swap (T& a, T& b) { //some stuff
}

int main ()
{
int a, b;
swap (a, b); //Which swap to call?
}


VERSUS:
--------

#include <algorithm>
#include <functional>

template <class T>
void swap (T& a, T& b) { //some stuff
}


int main ()
{
int a, b;
swap (a, b); //Ok, call the global swap.
std::swap (a, b); //Ok, call std::swap.
}




Regards,
-Dhruv.






[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by kanz » Fri, 24 Oct 2003 03:20:13


I like your toolbox/workbench analogy, but you don't cover what I find
to be a common case: you know that you are going to need the hacksaw and
the hammer quite a few times in the next bit of work, so you get those
two (and only those two) out and put them on the workbench. In C++, a
scoped:

using std::hacksaw ;
using std::hammer ;

I find that if you start doing this, there is no real motivation for a
global "using namespace std" anyway.

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

"using namespace std" Vs "std::..."

Post by Ben Hutchi » Fri, 24 Oct 2003 03:40:27


What it does is more like adding that namespace to a list of
namespaces to search for unqualified names. Any names added to that
namespace later will also become available in the namespace of the
using directive.

<snip>

This is not an error. The potential problem is ambiguity in
other namespaces:

#include <map>

using namespace std;

int pair(int x) // OK
{
return x * 2;
}

namespace foo
{
// Here, namespace std and the global namespace have equal
// priority in unqualified name lookup.

int twice(int x)
{
return pair(x); // ERROR: ambiguity
}
}

[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]