A namespace is a group of related terms, classes, or attributes that exist within your project. Usually, namespaces have some sort of contextual relevance that influences their structure.
When we start writing any program, we add a pre-processor directive, but it does not end here. Our basics for writing a code include much more than just a pre-processor directive or datatypes. There is a lot more that we might sometimes ignore but are necessary to learn, and one of them is Namespace.
What is Namespace?
A namespace provides scope and order to the identifiers in your code. It is a declarative region that allows us to create logically separate namespaces, thereby preventing name collisions when we include multiple libraries.
For example, if two libraries declare a class named “vehicle” you can use namespaces to ensure that one version does not override the other when imported into your code.
All identifiers at namespace scope are visible to one another without qualification. Identifiers outside the Namespace can access the members using directive for all the identifiers in the Namespace (using namespace std;).
Example
#include <iostream> //Example of inclusion directive
using namespace std;
int main(){
std::cout << "Welcome to Geek on Peak" ;
}
Why Using Directive?
A using-directive allows using all the names in a namespace without using the namespace name as an explicit qualifier. If you are using several different identifiers in a namespace; or just using one or two identifiers, then consider the using-declaration only to bring those identifiers into the scope and not all the identifiers in the Namespace.
Example
#include <iostream>
using namespace std;
// first name space
namespace first {
void func() {
std::cout << "Inside the first namespace" << endl;
}
}
// second name space
namespace second {
void func() {
std::cout << "Inside the second namespace" << endl;
}
}
using namespace first;
int main () {
func(); // This calls function from first name space.
return 0;
}
Global Namespace
In C++, they are explicit, meaning that a user or the programmer can create many namespaces in one program. As a result, it cannot be easy to understand code. The namespace operator allows you to explicitly( By the user) declare the current Namespace or separate declarations into different namespaces with little overhead; Instead of declaring them global (implicitly), you just put the namespace scope operator(::) before your symbol.
Std Namespace
All C++ standard library types and functions declared in the std namespace or namespaces nested inside std. The standard library is in the std namespace, which is short for “standard.”
Nested Namespace
As nested means that it is one inside the other, nested namespaces help organize code in large projects and avoid naming collisions with other software projects. An ordinary nested namespace has unqualified access to its parent’s members, but the parent members do not have unqualified access to the nested Namespace (unless inline is declared).
Example
#include <iostream>
using namespace std;
// first name space
namespace first {
void func() {
std::cout << "Inside the first namespace" << endl;
}
// second name space
namespace second {
void func() {
std::cout << "Inside the second namespace" << endl;
}
}
}
using namespace first::second;
int main () {
func(); // This calls function from second namespace.//
return 0;
}
Inline Namespace
In an inline namespace, it treats members as if they were members of the parent namespace. This characteristic enables argument-dependent lookup to work on functions that have overloads in a parent and a nested inline, as well as allows you to declare a specialization in a parent namespace for a template that we declare in the inline.
Syntax
inline namespace ns-name
{ declarations }
FOLLOW-UP
So, You learned about Namespaceand Directives very effectively. If you understood and comprehended this blog properly, well done, you’re getting advanced daily. If, in any case, you’re finding it difficult, you can let us know in the comments & contact us. Besides, if you’ve any doubts, you can post them in the comments. Don’t worry; our geeks will surely get your doubts cleared soon.
Similarly, you must also know that GeekonPeak has just begun, and it is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more extensive and easy-to-grasp courses for you (for free). You can follow us on Facebook, Instagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs and tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners who have excellent experiences to share with you. Until next time 🙂
2 Comments
Michael
In what cases would you use the inline namespace ?
Kritish
Hey Michel,
Thanks for putting up the query!
As you must know, Namespaces provide the space to declare variables.
We can also use this concept and make it nested, nested namespaces.
Here things go hierarchical, as we can create various namespaces using inline namespace, and the variable’s scope also becomes limited within the parent namespace.
If you go nested with an inline namespace, for instance, you’ve created a namespace ‘ns1’, and inside the ‘ns1’, you’ve created another namespace ‘ns_inside’, and to make it easier, you’ve also declared an integer num variable of 10 inside the ‘ns_inside’, then the structure of code would look like:
ns1 -> ns_inside -> num
OR
First Namespace -> inline (nested) namespace -> integer variable
And the code would look like this:
Inline Namespace Example Code (OnlineGDB)
Thanks for putting up this query; this is the best and easiest way; I can explain this topic in the comments.
Unfortunately, we don’t have an alternate method for doubts resolution at the very moment.
Besides, we’re also working on solving the doubts and queries more efficiently.
This is an advanced topic; we’ll refine it and shift it into the intermediate C++ section soon.
You can read more about this topic in detail Right Here:
C++ Inline Namespaces (GeeksforGeeks)
Hope you will appreciate the effort.
If you do,
You can share this website, Subscribe to our newsletters and follow GeekonPeak on Socials 🙂