using namespace std?

Started by
5 comments, last by mikeman 17 years, 9 months ago
ive seen in many codes the line using namespace std; so my question is what does it do? and how can it help me in my programs?
Advertisement
Namespaces in C++ are logical "units" that can contain functions and classes. It's a way of organizing large projects. For example:

namespace foo
{
void bar()
{

}
}

The function "bar" is part of the "foo" namespace. It is accessed like this:

foo::bar();

If you don't want to write the namespace each time you use one of its members, you can use the "using namespace" directive.

using namespace foo;
bar();

Now, namespace "std" is just the namespace where all the objects of the STL(Standard Template Library) reside.
Quote:Original post by mikeman
Now, namespace "std" is just the namespace where all the objects of the STL(Standard Template Library) reside.


Correction: std is the namespace where all the elements of the Standard C++ Library are located.

Quote:so my question is what does it do?


A using directive imports all the names of the specified namespace into the current namespace (usually the global namespace), allowing them to be used without full qualification. As mikeman indicated, it allows you to write cout instead of std::cout.

Quote:and how can it help me in my programs?


Its most basic use is just to save typing in thos cases where you know there isn't going to be a name conflict. That is, if you don't already have a class named string in your code, you can do using namespace std; and refer to std::string as string. If you do already have such a class, you can't do it.

A more advanced use is to import a namespace into another:
namespace bar{   void func() {};}namespace foo{   using namespace bar;}


Now you can use foo::func() to refer to bar::func().

Though usually you explicitely import specific components rather than whole namespaces at one.

#include <fruny/string.hpp>#include <vector>namespace lib{   using fruny::string;   using std::vector;};


lib::string will be fruny::string, but lib::vector will be std::vector.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:
Correction: std is the namespace where all the elements of the Standard C++ Library are located.


Ok, ok![smile] I guessed I'm used to calling it STL(writing SC++L looks a little weird).

Quote:Original post by mikeman
Ok, ok![smile] I guessed I'm used to calling it STL(writing SC++L looks a little weird).


There is more to the standard C++ library than just the STL. [smile] IOStreams, though now built as templates, were never part of it. Neither were strings, although the standard C++ strings are containers in the traditional STL sense.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Fruny thanks :]
Quote:Original post by Fruny
Quote:Original post by mikeman
Ok, ok![smile] I guessed I'm used to calling it STL(writing SC++L looks a little weird).


There is more to the standard C++ library than just the STL. [smile] IOStreams, though now built as templates, were never part of it. Neither were strings, although the standard C++ strings are containers in the traditional STL sense.


I'm perfectly aware of all that, it's just a bad habit of mine(and many others) to just call the whole thing STL.

This topic is closed to new replies.

Advertisement