Namespace, What Is It

I am trying to learn C++ and am working my way through a Dummies book with that title. I am up to arrays and have noticed that most of the code examples have the line below right after the #include statements. I understand that the #include statements tell the compiler what library routines are used, but just what does this line do? And just what is "namespace"? Is it also some kind of library? Or is it something that is built in to C++? Or what?

Oh, and what does "std" mean or stand for?

I like to understand these things.

 
  using namespace std;
Last edited on
Like area code for phone numbers.
I read the page referenced. It seems to allow the same names to be used in different parts of a program for different purposes and with different values. Hence, the somewhat vague reference to area codes.

But I still do not know what it is. Is it a compiler directive? Or something else?

And, in the sample programs in the book, I do not see any use of the

namespace myNamespace

lines that the explanation page shows to declare the various namespaces. So the author appears not to be using namespaces. Is this line (using namespace std;) required for a C++ program or is it optional, but just included in case it will be used? It seems to be included in almost all of the sample programs in this book.
What do you mean by "compiler directive"?

A namespace, like a class, introduces a new scope. But that's where the similarities end. Namespaces are namespaces - they are their own kind of 'thing'.

The line using namespace std; is actually a bad habit, and you should not use it. It completely circumvents the point of namespaces. Lazy people use it in example code because they're too lazy to not use it.
Last edited on
Hi,

Just a little bit extra explanation ...

The std namespace has the whole of the STL (Standard Template Library) in it. So it has lots of templates, classes, functions & algorithms to make life easier. For example (to mention a couple of examples out of 100's that are in there)it has std::string which is a class that allows one to easily do lots of stuff with strings. With algorithms there is std::sort, std::find. The classes all define their own functions and overload operators to provide lots of functionality. With templates, these allow containers like std::vector to hold objects of any type, whether they be int, double, std::string or some other class that you invented yourself.

There are other libraries - if you have a look at boost you will see that it has lots of different namespaces that are often nested.

And yes it is a very good idea to put your own stuff in it's own namespace

The main problem with using namespace std is how easy it is to inadvertently create a naming conflict which will give lots of errors that may be quite difficult to sort out. For example here are a few out of many that cause problems, one might have these variables: left, right, distance, ratio; but not realise that they all exist in std::. And I reckon it would be bad form to name them Left, Right, Distance, Ratio to avoid the problem - As LB says: don't do it in the first place. I wish that book authors would quit doing it.

I hope this gives the detail to aid your understanding, and all is well at your end :+)
> But I still do not know what it is. Is it a compiler directive?

Yes, it is a compiler directive. It is called a using-directive.

Namespaces in a C++ program are akin to directories (folders) in a file system. Just as files (or subdirectories) can be placed in a directory, names of C++ entities (including those of nested namespaces) can be placed in a namespace.

All the names exposed by the C++ standard library is in one namespace (directory) called std.

One way to get to these names is to qualify the name with the name of the namespace:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
{
    std::string name ; // the name string that is in the namespace std
    std::cin >> name ; // the name cin that is in the namespace std
    std::cout << "Hello, " << name << "!\n" ; // the name cout that is in the namespace std
}


Another way to get to these names is to use unqualified names (just cin, cout etc.), but inform the compiler beforehand what these unqualified names really mean. We can do this with a using-declaration.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
    using std::string ; // when I say string, I really mean std::string (using-declaration)
    using std::cin ; // (using-declaration)
    using std::cout ; // (using-declaration)
    
    string name ; // ok; the unqualified name string means the name string 
                  // which is in namespace std (std::string)
    cin >> name ; // the name cin that is in the namespace std (std::cin)
    cout << "Hello, " << name << "!\n" ; // the name cout that is in the namespace std (std::cout)
}


A third way is to give a directive to the compiler: if an unqualified name can't be located locally, then look into a particular namespace to see if it can be found there. We can do that with a using-directive.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
    using namespace std ; // If I use an unqualified name, and it can't be located,
                          // look into the namesapce std and pick it up from there.
                          // (using-directive)  
                           
    string name ; // ok; the unqualified name string is not found using 'normal' lookup
                  // so, look in namespace std, and pick up std::string
    cin >> name ; // look in namespace std to resove cin => (std::cin)
    cout << "Hello, " << name << "!\n" ; // cout => (std::cout)
}


Every program starts off with one predefined namespace; all names (including names of other namespaces) are within this global namespace. There is an implicit using-directive for the global namespace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

std::string i = "hello" ; // name 'i' is in the global namespace

namespace my // name 'my' is in the global namespace
{
    std::string j = "world" ; // name 'j' is in the namespace 'my'
}

int main()
{
    using namespace std ; // (using-directive)
    using namespace my ; // (using-directive)
          
    cout /* std::cout */ << i /* ::i */ << ' ' << j /* my::j */ << '\n' ; // hello world

    unsigned int cout = 0, i = 1, j = 2 ; // intoduce names cout, i, j into the current scope
    cout = i + j ; // operations on variables of type unsigned int. 
                   // using-directives in effect were not needed to resolve these unqualified names

   std::cout << cout << '\n' ; // 3 
}
Great information. Thanks.

I am going to have to study the answers for a while.

I apologize if I misused the term "compiler directive" but when you are starting out and struggling, you have to use the words you have to ask the questions. By this I meant a line in the source code that does not generate any machine code, but instead tells the C++ compiler something that it needs to know in order to properly generate the code. I have seen this term used with other languages, but perhaps it is called something else in C++. Please be patient with me, I am trying to learn.

Perhaps I am handicapped by my knowledge of other languages. I had my first introduction to computer programming in the 1960s with Fortran. By comparison, C++ is a very rich environment.
Is it a compiler directive?

Well, in the broadest sense, every statement you write directs the compiler to do something. In a much narrower sense, in C/C++ compiler directives are generally considered to be those lines that begin with #. e.g. #include, #define, #pragma, etc.

No need to apologize for calling it a compiler directive. You got you question across effectively.

You can think of namespace as a way of giving a name to a scope. When you use a pair of braces, you're introducing a new scope to the compiler. namespace is simply a way of giving a name to a set of variables, classes, or functions included within the scope of that namespace. As pointed out before, namespaces are a way of preventing naming conflicts, as long as namespace names are unique.

As TheIdeasMan mentioned, the std namespace contains commonly used names such as sort and find. If you had your own sort and find and also had using namespace std; the compiler would not know which one you were referring to. This is why including using namespace std; is not a good idea. By qualifying your reference as std::sort, the compiuler knows you want the sort function in the std namespace. You can of course, qualify the reference to your version as ::sort and the compiler will know to use the version in the global namespace. Or you can avoid the ambiguity by placing your sort function in your own namespace.

To add to JLBorges excellent examples, there is one more example, namespace aliases. aliases are simply a shorthand way to refer to a more complicated set of namespace names.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
namespace foo {
    namespace bar {
         namespace baz {
             int qux = 42;
         }
    }
}
 
namespace fbz = foo::bar::baz;
 
int main()
{
    std::cout << fbz::qux << '\n';
}

AbstractionAnon wrote:
in C/C++ compiler directives are generally considered to be those lines that begin with #. e.g. #include, #define, #pragma, etc.

Those are usually called preprocessor directives.
Just to clear things up: the reason I asked "What do you mean by 'compiler directive'?" was not because I thought the term was being used incorrectly, it was because I wanted to make sure the OPer and I were on the same page.
Topic archived. No new replies allowed.