C++C++11C++14C++17Introduction to C++Learn C++

What is the std namespace in C++?

What is the std namespace in C++

What is namespace std in C++? What does namespace mean for a C++ compiler? How can we define a namespace in C++ IDE? Why don’t we use “using namespace std;” in C++ Builder? How can I use the std namespace? How can I define my own namespace? Let’s answer these questions.

What is a namespace in C++?

Namespaces in C++ (namespace) allow users to group named entities like classes, methods, variables and functions in a narrower namespace scope instead of using in the global scope. This prevents name conflicts between classes, methods, functions and variables in large projects. Namespaces organize the elements of developers into different logical scopes referred to by names. Thus, different users (or namespaces) may use same method names and variables in different running processes.

In brief,

  • A namespace is a declarative region that provides a scope to the identifiers inside it.
  • All declarations within namespaces are declared in the named scope.
  • Same names of entities can be used in multiple namespaces.
  • Namespace is a scope space in C++ only and not present in C.

A namespace declaration;

  • appears only at global scope.
  • can be nested within another namespace.
  • doesn’t have access specifiers like public, private in classes

Also note that, you don’t need to give semicolon after the closing brace of definition of namespace. You can split the definition of namespace over several units.

How Can I Use std Entities Without Using “using namespace std;” ?

The using namespace std; line tells the compiler “Take everything that’s in the std namespace and dump it in the global namespace”. Thus, you can refer to std entities without the std:: prefix, but it increases the probability for name conflicts, since a bunch of extra names that you didn’t expect also got added to the global namespace.

std is a standard namespace that holds many C++ classes and methods like cout, cin, among others. We can use classes or methods of this std namespace like the example below,

As in this example “std” a standard C++ namespace and the “::” operator is the scope operator. In other terms, it tells the compiler which class/namespace to look in for an identifier. Here cout is a class method in std namespace that prints out the next coming statements

How can I use the std namespace? “using namespace std;” ?

Generally namespaces are declared in beginning of the program, after header libraries. Here is an example how to use std

How can I define my own namespace in C++?

We can define our own namespaces that may prevents conflicts of entity names (classes, methods, variables). Here is the example with two nsA and nsB namespaces defined by developer and both has same myf() functions used in different processes.

As you see we have two different myf() functions but we can use both of them by pointing their namespace with the scope operator as in nsA::myf() and nsB::myf(); lines.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?