7.2 — User-defined namespaces and the scope resolution operator

In lesson 2.9 -- Naming collisions and an introduction to namespaces, we introduced the concept of naming collisions and namespaces. As a reminder, a naming collision occurs when two identical identifiers are introduced into the same scope, and the compiler can’t disambiguate which one to use. When this happens, compiler or linker will produce an error because they do not have enough information to resolve the ambiguity.

Key insight

As programs become larger, the number of identifiers increases, which in turn causes the probability of a naming collision occurring to increase significantly. Because every name in a given scope can potentially collide with every other name in the same scope, a linear increase in identifiers will result in an exponential increase in potential collisions! This is one of the key reasons for defining identifiers in the smallest scope possible.

Let’s revisit an example of a naming collision, and then show how we can improve things using namespaces. In the following example, foo.cpp and goo.cpp are the source files that contain functions that do different things but have the same name and parameters.

foo.cpp:

// This doSomething() adds the value of its parameters
int doSomething(int x, int y)
{
    return x + y;
}

goo.cpp:

// This doSomething() subtracts the value of its parameters
int doSomething(int x, int y)
{
    return x - y;
}

main.cpp:

#include <iostream>

int doSomething(int x, int y); // forward declaration for doSomething

int main()
{
    std::cout << doSomething(4, 3) << '\n'; // which doSomething will we get?
    return 0;
}

If this project contains only foo.cpp or goo.cpp (but not both), it will compile and run without incident. However, by compiling both into the same program, we have now introduced two different functions with the same name and parameters into the same scope (the global scope), which causes a naming collision. As a result, the linker will issue an error:

goo.cpp:3: multiple definition of `doSomething(int, int)'; foo.cpp:3: first defined here

Note that this error happens at the point of redefinition, so it doesn’t matter whether function doSomething is ever called.

One way to resolve this would be to rename one of the functions, so the names no longer collide. But this would also require changing the names of all the function calls, which can be a pain, and is subject to error. A better way to avoid collisions is to put your functions into your own namespaces. For this reason the standard library was moved into the std namespace.

Defining your own namespaces

C++ allows us to define our own namespaces via the namespace keyword. Namespaces that you create in your own programs are casually called user-defined namespaces (though it would be more accurate to call them program-defined namespaces).

The syntax for a namespace is as follows:

namespace NamespaceIdentifier
{
    // content of namespace here
}

We start with the namespace keyword, followed by an identifier for the namespace, and then curly braces with the content of the namespace inside.

Historically, namespace names have not been capitalized, and many style guides still recommend this convention.

For advanced readers

Some reasons to prefer namespace names starting with a capital letter:

  • It is convention to name program-defined types starting with a capital letter. Using the same convention for program-defined namespaces is consistent (especially when using a qualified name such as Foo::x, where Foo could be a namespace or a class type).
  • It helps prevent naming collisions with other system-provided or library-provided lower-cased names.
  • The C++20 standards document uses this style.
  • The C++ Core guidelines document uses this style.

We recommend starting namespace names with a capital letter. However, either style should be seen as acceptable.

A namespace must be defined either in the global scope, or inside another namespace. Much like the content of a function, the content of a namespace is conventionally indented one level. You may occasionally see an optional semicolon placed after the closing brace of a namespace.

Here is an example of the files in the prior example rewritten using namespaces:

foo.cpp:

namespace Foo // define a namespace named Foo
{
    // This doSomething() belongs to namespace Foo
    int doSomething(int x, int y)
    {
        return x + y;
    }
}

goo.cpp:

namespace Goo // define a namespace named Goo
{
    // This doSomething() belongs to namespace Goo
    int doSomething(int x, int y)
    {
        return x - y;
    }
}

Now doSomething() inside of foo.cpp is inside the Foo namespace, and the doSomething() inside of goo.cpp is inside the Goo namespace. Let’s see what happens when we recompile our program.

main.cpp:

int doSomething(int x, int y); // forward declaration for doSomething

int main()
{
    std::cout << doSomething(4, 3) << '\n'; // which doSomething will we get?
    return 0;
}

The answer is that we now get another error!

ConsoleApplication1.obj : error LNK2019: unresolved external symbol "int __cdecl doSomething(int,int)" (?doSomething@@YAHHH@Z) referenced in function _main

In this case, the compiler was satisfied (by our forward declaration), but the linker could not find a definition for doSomething in the global namespace. This is because both of our versions of doSomething are no longer in the global namespace! They are now in the scope of their respective namespaces!

There are two different ways to tell the compiler which version of doSomething() to use, via the scope resolution operator, or via using statements (which we’ll discuss in a later lesson in this chapter).

For the subsequent examples, we’ll collapse our examples down to a one-file solution for ease of reading.

Accessing a namespace with the scope resolution operator (::)

The best way to tell the compiler to look in a particular namespace for an identifier is to use the scope resolution operator (::). The scope resolution operator tells the compiler that the identifier specified by the right-hand operand should be looked for in the scope of the left-hand operand.

Here is an example of using the scope resolution operator to tell the compiler that we explicitly want to use the version of doSomething() that lives in the Foo namespace:

#include <iostream>

namespace Foo // define a namespace named Foo
{
    // This doSomething() belongs to namespace Foo
    int doSomething(int x, int y)
    {
        return x + y;
    }
}

namespace Goo // define a namespace named Goo
{
    // This doSomething() belongs to namespace Goo
    int doSomething(int x, int y)
    {
        return x - y;
    }
}

int main()
{
    std::cout << Foo::doSomething(4, 3) << '\n'; // use the doSomething() that exists in namespace Foo
    return 0;
}

This produces the expected result:

7

If we wanted to use the version of doSomething() that lives in Goo instead:

#include <iostream>

namespace Foo // define a namespace named Foo
{
    // This doSomething() belongs to namespace Foo
    int doSomething(int x, int y)
    {
        return x + y;
    }
}

namespace Goo // define a namespace named Goo
{
    // This doSomething() belongs to namespace Goo
    int doSomething(int x, int y)
    {
        return x - y;
    }
}

int main()
{
    std::cout << Goo::doSomething(4, 3) << '\n'; // use the doSomething() that exists in namespace Goo
    return 0;
}

This produces the result:

1

The scope resolution operator is great because it allows us to explicitly pick which namespace we want to look in, so there’s no potential ambiguity. We can even do the following:

#include <iostream>

namespace Foo // define a namespace named Foo
{
    // This doSomething() belongs to namespace Foo
    int doSomething(int x, int y)
    {
        return x + y;
    }
}

namespace Goo // define a namespace named Goo
{
    // This doSomething() belongs to namespace Goo
    int doSomething(int x, int y)
    {
        return x - y;
    }
}

int main()
{
    std::cout << Foo::doSomething(4, 3) << '\n'; // use the doSomething() that exists in namespace Foo
    std::cout << Goo::doSomething(4, 3) << '\n'; // use the doSomething() that exists in namespace Goo
    return 0;
}

This produces the result:

7
1

Using the scope resolution operator with no name prefix

The scope resolution operator can also be used in front of an identifier without providing a namespace name (e.g. ::doSomething). In such a case, the identifier (e.g. doSomething) is looked for in the global namespace.

#include <iostream>

void print() // this print() lives in the global namespace
{
	std::cout << " there\n";
}

namespace Foo
{
	void print() // this print() lives in the Foo namespace
	{
		std::cout << "Hello";
	}
}

int main()
{
	Foo::print(); // call print() in Foo namespace
	::print();    // call print() in global namespace (same as just calling print() in this case)

	return 0;
}

In the above example, the ::print() performs the same as if we’d called print() with no scope resolution, so use of the scope resolution operator is superfluous in this case. But the next example will show a case where the scope resolution operator with no namespace can be useful.

Identifier resolution from within a namespace

If an identifier inside a namespace is used and no scope resolution is provided, the compiler will first try to find a matching declaration in that same namespace. If no matching identifier is found, the compiler will then check each containing namespace in sequence to see if a match is found, with the global namespace being checked last.

#include <iostream>

void print() // this print() lives in the global namespace
{
	std::cout << " there\n";
}

namespace Foo
{
	void print() // this print() lives in the Foo namespace
	{
		std::cout << "Hello";
	}

	void printHelloThere()
	{
		print();   // calls print() in Foo namespace
		::print(); // calls print() in global namespace
	}
}

int main()
{
	Foo::printHelloThere();

	return 0;
}

This prints:

Hello there

In the above example, print() is called with no scope resolution provided. Because this use of print() is inside the Foo namespace, the compiler will first see if a declaration for Foo::print() can be found. Since one exists, Foo::print() is called.

If Foo::print() had not been found, the compiler would have checked the containing namespace (in this case, the global namespace) to see if it could match a print() there.

Note that we also make use of the scope resolution operator with no namespace (::print()) to explicitly call the global version of print().

Forward declaration of content in namespaces

In lesson 2.11 -- Header files, we discussed how we can use header files to propagate forward declarations. For identifiers inside a namespace, those forward declarations also need to be inside the same namespace:

add.h

#ifndef ADD_H
#define ADD_H

namespace BasicMath
{
    // function add() is part of namespace BasicMath
    int add(int x, int y);
}

#endif

add.cpp

#include "add.h"

namespace BasicMath
{
    // define the function add() inside namespace BasicMath
    int add(int x, int y)
    {
        return x + y;
    }
}

main.cpp

#include "add.h" // for BasicMath::add()

#include <iostream>

int main()
{
    std::cout << BasicMath::add(4, 3) << '\n';

    return 0;
}

If the forward declaration for add() wasn’t placed inside namespace BasicMath, then add() would be declared in the global namespace instead, and the compiler would complain that it hadn’t seen a declaration for the call to BasicMath::add(4, 3). If the definition of function add() wasn’t inside namespace BasicMath, the linker would complain that it couldn’t find a matching definition for the call to BasicMath::add(4, 3).

Multiple namespace blocks are allowed

It’s legal to declare namespace blocks in multiple locations (either across multiple files, or multiple places within the same file). All declarations within the namespace are considered part of the namespace.

circle.h:

#ifndef CIRCLE_H
#define CIRCLE_H

namespace BasicMath
{
    constexpr double pi{ 3.14 };
}

#endif

growth.h:

#ifndef GROWTH_H
#define GROWTH_H

namespace BasicMath
{
    // the constant e is also part of namespace BasicMath
    constexpr double e{ 2.7 };
}

#endif

main.cpp:

#include "circle.h" // for BasicMath::pi
#include "growth.h" // for BasicMath::e

#include <iostream>

int main()
{
    std::cout << BasicMath::pi << '\n';
    std::cout << BasicMath::e << '\n';

    return 0;
}

This works exactly as you would expect:

3.14
2.7

The standard library makes extensive use of this feature, as each standard library header file contains its declarations inside a namespace std block contained within that header file. Otherwise the entire standard library would have to be defined in a single header file!

Note that this capability also means you could add your own functionality to the std namespace. Doing so causes undefined behavior most of the time, because the std namespace has a special rule prohibiting extension from user code.

Warning

Do not add custom functionality to the std namespace.

Nested namespaces

Namespaces can be nested inside other namespaces. For example:

#include <iostream>

namespace Foo
{
    namespace Goo // Goo is a namespace inside the Foo namespace
    {
        int add(int x, int y)
        {
            return x + y;
        }
    }
}

int main()
{
    std::cout << Foo::Goo::add(1, 2) << '\n';
    return 0;
}

Note that because namespace Goo is inside of namespace Foo, we access add as Foo::Goo::add.

Since C++17, nested namespaces can also be declared this way:

#include <iostream>

namespace Foo::Goo // Goo is a namespace inside the Foo namespace (C++17 style)
{
    int add(int x, int y)
    {
        return x + y;
    }
}

int main()
{
    std::cout << Foo::Goo::add(1, 2) << '\n';
    return 0;
}

This is equivalent to the prior example.

If you later need to add declarations to the Foo namespace (only), you can define a separate Foo namespace to do so:

#include <iostream>

namespace Foo::Goo // Goo is a namespace inside the Foo namespace (C++17 style)
{
    int add(int x, int y)
    {
        return x + y;
    }
}

namespace Foo
{
     void someFcn() {} // This function is in Foo only
}

int main()
{
    std::cout << Foo::Goo::add(1, 2) << '\n';
    return 0;
}

Whether you keep the separate Foo::Goo definition or nest Goo inside Foo is a stylistic choice.

Namespace aliases

Because typing the qualified name of a variable or function inside a nested namespace can be painful, C++ allows you to create namespace aliases, which allow us to temporarily shorten a long sequence of namespaces into something shorter:

#include <iostream>

namespace Foo::Goo
{
    int add(int x, int y)
    {
        return x + y;
    }
}

int main()
{
    namespace Active = Foo::Goo; // active now refers to Foo::Goo

    std::cout << Active::add(1, 2) << '\n'; // This is really Foo::Goo::add()

    return 0;
} // The Active alias ends here

One nice advantage of namespace aliases: If you ever want to move the functionality within Foo::Goo to a different place, you can just update the Active alias to reflect the new destination, rather than having to find/replace every instance of Foo::Goo.

#include <iostream>
 
namespace Foo::Goo
{
}

namespace V2
{
    int add(int x, int y)
    {
        return x + y;
    }
}
 
int main()
{
    namespace Active = V2; // active now refers to V2
 
    std::cout << Active::add(1, 2) << '\n'; // We don't have to change this
 
    return 0;
}

How to use namespaces

It’s worth noting that namespaces in C++ were not originally designed as a way to implement an information hierarchy -- they were designed primarily as a mechanism for preventing naming collisions. As evidence of this, note that the entirety of the standard library lives under the single top-level namespace std. Newer standard library features that introduce lots of names have started using nested namespaces (e.g. std::ranges) to avoid naming collisions within the std namespace.

  • Small applications developed for your own use typically do not need to be placed in namespaces. However, for larger personal projects that include lots of third party libraries, namespacing your code can help prevent naming collisions with libraries that aren’t properly namespaced.

Author’s note

The examples in these tutorials will typically not be namespaced unless we are illustrating something specific about namespaces, to help keep the examples concise.

  • Any code that will be distributed to others should definitely be namespaced to prevent conflicts with the code it is integrated into. Often a single top-level namespace will suffice (e.g. Foologger). As an additional advantage, placing library code inside a namespace also allows the user to see the contents of your library by using their editor’s auto-complete and suggestion feature (e.g. if you type Foologger, autocomplete will show you all of the names inside Foologger).
  • In multi-team organizations, two-level or even three-level namespaces are often used to prevent naming conflicts between code generated by different teams. These often take the form of one of the following:
  1. Project or library :: module (e.g. Foologger::Lang)
  2. Company or org :: project or library (e.g. Foosoft::Foologger)
  3. Company or org :: project or library :: module (e.g. Foosoft::Foologger::Lang)

Use of module-level namespaces can help separate code that might be reusable later from application-specific code that will not be reusable. For example, physics and math functions could go into one namespace (e.g. Math::). Language and localization functions in another (e.g. Lang::). However, directory structures can also be used for this (with app-specific code in the project directory tree, and reusable code in a separate shared directory tree).

In general, you should avoid deeply nested namespaces (more than 3 levels).

Related content

C++ provides other useful namespace functionality. We cover unnamed namespaces and inline namespaces later in this chapter, in lesson 7.13 -- Unnamed and inline namespaces.

guest
Your email address will not be displayed
Find a mistake? Leave a comment above!
Correction-related comments will be deleted after processing to help reduce clutter. Thanks for helping to make the site better for everyone!
Avatars from https://gravatar.com/ are connected to your provided email address.
Notify me about replies:  
427 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments