← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 5 of 17

C++ Namespaces

A namespace is a named group that keeps your names separate from others, like putting your toys in a box with your name on it. It avoids mix-ups when names match.
Syntax
cpp
namespace NamespaceName {
  return_type function_name() { }
}

NamespaceName::function_name();

using namespace NamespaceName;

What is a Namespace?

A namespace is a feature in C++ used to group related code under a unique name. It helps prevent naming conflicts. This is especially useful when your project uses multiple external libraries with identical class or function names.

Example: What is a Namespace?

cpp
// Include std::cout and std::cin
#include <iostream>

namespace MyLibrary {
	void greet() { std::cout << "Hello from MyLibrary" << std::endl; }
}

// Program execution starts in main()
int main() {
	MyLibrary::greet();
	// Return 0 to signal that the program finished successfully
	return 0;
}

The Standard Namespace

All standard library templates, classes, and objects are grouped inside the std namespace. You can use the scope resolution operator or write a using directive to import standard elements into your current scope.

Example: The Standard Namespace

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Using the std namespace" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Namespaces

You can declare a namespace inside another namespace. This establishes a clear hierarchy for your code modules, similar to nested folders on your computer.

Example: Nested Namespaces

cpp
// Include std::cout and std::cin
#include <iostream>

namespace Company {
	namespace Project {
		void run() { std::cout << "Running" << std::endl; }
	}
}

// Program execution starts in main()
int main() {
	Company::Project::run();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Inline Namespaces

An inline namespace allows you to define versioned APIs. Members of an inline namespace are automatically visible in the parent namespace. This helps you deploy code updates while preserving backward compatibility.

Example: Inline Namespaces

cpp
// Include std::cout and std::cin
#include <iostream>

namespace Library {
	inline namespace V2 {
		void greet() { std::cout << "Version 2" << std::endl; }
	}
}

// Program execution starts in main()
int main() {
	Library::greet();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Namespaces Best Practices

Avoid putting global using directives in header files. This can pollute the global scope of any file that includes your header, leading to hard-to-track naming conflicts.

Example: Namespaces Best Practices

cpp
#include <iostream>

// Avoid "using namespace std;" in header files -- it would pollute the
// global scope of every file that includes this header.

int main() {
	std::cout << "Prefer explicit std:: prefixes in headers" << std::endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting :: when calling a function from a namespace.
  2. Creating name conflicts by using using namespace everywhere.
  3. Putting using namespace std; in a header file.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.