Quick Introduction to namespaces in C++

As someone who writes C++ code often, I see (and use) the keyword namespace frequently and I think it is worth knowing what this keyword is all about.

A namespace is a region that provides a scope to the identifiers (the names of functions, variables,…


This content originally appeared on DEV Community and was authored by Aastha Gupta

As someone who writes C++ code often, I see (and use) the keyword namespace frequently and I think it is worth knowing what this keyword is all about.

A namespace is a region that provides a scope to the identifiers (the names of functions, variables, etc) inside it. namespaces are used to organize code into logical groups. All identifiers in the namespace scope are visible to one another.

The identifiers in the namespace can be accessed by bringing those said identifiers into the current scope by using or utilizing fully qualified names.

using namespace std;
// the namespace std is now in scope
string my_string;

std::string my_string
// fully qualified name

Declaring namespace and namespace members

A namespace declaration is generally done in a header file as following:

// my_namespace_declaration.h

namespace my_own_namepsace
{
    void func_one();
    int func_two();
}

The members of this namespace can then be brought into scope of any other file by fully qualified names or the using directive. (If you want to know more about using, checkout this article by me)

// other_file.cpp

#include"my_own_namepsace.h"
using namespace my_own_namepsace;

my_own_namepsace::func_one();
// fully qualified names are to be used
// even when bringing the namespace in 
// scope with using directive

Code in a header file that uses identifiers from other namespaces should always use the fully qualified namespace name to avoid ambiguity. This isocpp core guideline puts more light.

Nested namespaces

namespaces can be nested. A nested namespace has unqualified access to its parent's members however the parent members do not have unqualified access to the nested namespace (unless it is declared as inline).

The std namespace

All C++ standard library types and functions are declared in the std namespace or the various namespaces that are nested inside std which makes it one of the most common namespaces to be encountered.

This article is a short introduction and if you want to know more about namespaces, I encourage you to head over here.

Thanks for giving this article a read and I'll see you in the next one ?

PS: This is an article in my series Quick Introduction to a concept in C++. You can find all the articles in this series here.


This content originally appeared on DEV Community and was authored by Aastha Gupta


Print Share Comment Cite Upload Translate Updates
APA

Aastha Gupta | Sciencx (2021-06-17T15:43:43+00:00) Quick Introduction to namespaces in C++. Retrieved from https://www.scien.cx/2021/06/17/quick-introduction-to-namespaces-in-c/

MLA
" » Quick Introduction to namespaces in C++." Aastha Gupta | Sciencx - Thursday June 17, 2021, https://www.scien.cx/2021/06/17/quick-introduction-to-namespaces-in-c/
HARVARD
Aastha Gupta | Sciencx Thursday June 17, 2021 » Quick Introduction to namespaces in C++., viewed ,<https://www.scien.cx/2021/06/17/quick-introduction-to-namespaces-in-c/>
VANCOUVER
Aastha Gupta | Sciencx - » Quick Introduction to namespaces in C++. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/17/quick-introduction-to-namespaces-in-c/
CHICAGO
" » Quick Introduction to namespaces in C++." Aastha Gupta | Sciencx - Accessed . https://www.scien.cx/2021/06/17/quick-introduction-to-namespaces-in-c/
IEEE
" » Quick Introduction to namespaces in C++." Aastha Gupta | Sciencx [Online]. Available: https://www.scien.cx/2021/06/17/quick-introduction-to-namespaces-in-c/. [Accessed: ]
rf:citation
» Quick Introduction to namespaces in C++ | Aastha Gupta | Sciencx | https://www.scien.cx/2021/06/17/quick-introduction-to-namespaces-in-c/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.