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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++

C++C++17Introduction to C++IteratorsLearn C++Syntax

What Are The Differences Between Vector And Array In C++?