Namespaces

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Namespaces

What are namespaces?

Namespaces are a feature of C++. We already used them when we used the standard C++ library. For example, the std:: part of std::cout is there to specify that the object cout is part of the namespace std. All the elements of the C++ Standard Library are defined under the namespace std (Which stands for standard, if you have not guessed already.). Namespaces are tools used to avoid name collisions. Name collision happens when two elements have the same name. Example: We defined a function named initialize and we include a library that also defined a function of the same name (That takes the same arguments). That would cause an error, because the compiler would not know which function we are referring to when writing the name initialize. However, if we defined our function under a namespace named, let us say, my, then there would not have been any problem since our function would be referred to as my::initialize.

First, here is an example of a program with name collision:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> void func() { std::cout << "Hello, I am func.\n"; } void func() // Error: func has already been defined/declared. { std::cout << "No. I am func.\n"; } int main() { func(); return 0; }

Putting an element inside a namespace

Now, let us resolve the problem of the example above by putting one of the functions inside a namespace. To define elements under a namespace, we must write the keyword namespace followed by the name of the namespace and then add the element declarations/definitions inside the brackets {} following it. It the code below, we put the first function of the example above under the namespace my.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> namespace my{ void func() { std::cout << "Hello, I am func.\n"; } } void func() { std::cout << "No. I am func.\n"; } int main() { my::func(); // "Hello, I am func." is outputted to the console. func(); // "No. I am func." is printed into the console. return 0; }

Inline namespaces

What happens if both functions named func are defined under different namespaces and we try to call func without using any namespace name?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> namespace my{ void func() { std::cout << "Hello, I am func.\n"; } } namespace ns{ void func() { std::cout << "No. I am func.\n"; } } int main() { func(); // Error: A function with the name 'func' alone (Inside no namespace) does not exist. return 0; }

What happens is a compilation error. However, by putting the keyword inline before the namespace keyword, it is possible to make it the default namespace to be used when none is specified.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> inline namespace my{ void func() { std::cout << "Hello, I am func.\n"; } } namespace ns{ void func() { std::cout << "No. I am func.\n"; } } int main() { func(); // "Hello, I am func." is written to the console. return 0; }

Namepaces inside namespaces

We can define namespaces inside namespaces. If an element called elem is defined inside the namespace ns2, which is defined inside the namespace ns1, then the element elem is accessed as follows: ns1::ns2::elem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> namespace my{ namespace ns{ void func() { std::cout << "Hello, I am func.\n"; } } } int main() { my::ns::func(); // "Hello, I am func." is printed in the console. return 0; }

Redefining a namespace

We can redefine a namespace as many times as we want.

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> namespace my{ void func() { std::cout << "Hello, I am func.\n"; } } namespace my{ void func2() { std::cout << "Hello, I am func2.\n"; } } int main() { my::func(); // "Hello, I am func." is outputted to the console. my::func2(); // "Hello, I am func2." is written into the console. return 0; }

Defining a function outside of the namespace

We can declare a function inside a namespace and then define it outside by referring to it using the name of the namespace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> namespace my{ void func(); } void my::func() { std::cout << "Hello, I am my::func.\n"; } int main() { my::func(); // "Hello, I am my::func." is printed to the console. return 0; }

Naming convention

In this course, namespaces are written completely in lowercase and are named as an acronym (My Library -> ml).