C++Builder What is the std namespace in C++?

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
What is the std namespace in C++?
By Yilmaz Yoru January 6, 2022

What is namespace std in C++? What does namespace mean for a C++ compiler? How can we define a namespace in Для просмотра ссылки Войди или Зарегистрируйся? 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,
C++:
#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
C++:
#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.
C++:
#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.