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.
Table of Contents
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,
1 2 3 4 5 6 7 8 9 |
#include <iostream> int main() { std::cout << "This is 'cout' from 'std' namespace\n"; return 0; } |
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
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { cout << "This is 'cout' from 'std' namespace\n"; return 0; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> namespace nsA { void myf() { std::cout << "this is myf() function in nsA namespace\n"; } } namespace nsB { void myf() { std::cout << "this is myf() function in nsB namespace\n"; } } int main() { // Let's use a method or function from nsA namespace nsA::myf(); // Let's use a method or function from nsB namespace nsB::myf(); return 0; } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition