C ++: Namespaces - How to use in the header and source files?

Consider a couple of two source files: an interface declaration file ( *.h or *.hpp ) and its implementation file ( *.cpp ).

Let the *.h file look like this:

 namespace MyNamespace { class MyClass { public: int foo(); }; } 

I saw two different methods for using namespaces in the source files:

*.cpp shows practice number 1:

 #include "MyClass.h" using namespace MyNamespace; int MyClass::foo() { ... } 

*.cpp shows practice number 2:

 #include "MyClass.h" namespace MyNamespace { int MyClass::foo() { ... } } 

My question is: Are there any differences between these two practices and are considered better than others?

+52
c ++ header-files namespaces
May 30 '12 at 12:45
source share
5 answers

From the point of view of code readability, it is probably better, in my opinion, to use method No. 2 for this reason:

You can be using multiple namespaces at a time, and any object or function written below this line can belong to any of these namespaces (prohibiting name conflicts). The flow around the entire file in the namespace block is more explicit and allows you to declare new functions and variables that belong to this namespace in the .cpp file, as well as

+41
May 30 '12 at 12:52
source share

The clearest option you did not show:

 int MyNamespace::MyClass::foo() { // ... } 

It is also very verbose; too much for most people. Since using namespace is a trick for name conflicts, at least in my experience, and should be avoided, except in very limited areas and places, I usually use your # 2.

+35
May 30 '12 at 13:30
source share

Are there differences between the two practices

Yes. # 1 and # 2 are examples using-directive and namespace definitions respectively. In this case, they are almost identical, but have different consequences. For example, if you enter a new identifier next to MyClass::foo , it will have a different scope:

# one:

 using namespace MyNamespace; int x; // defines ::x 

# 2:

 namespace MyNamespace { int x; // defines MyNamespace::x } 

considered better than others?

# 1 Pros: a bit more concise; it’s more difficult to accidentally enter something into MyNamespace involuntarily. Cons: inadvertently pull existing identifiers.

# 2 Pros: It is more clear that the definitions of existing identifiers and the declaration of new identifiers belong to MyNamespace . Cons: It is easier to inadvertently enter identifiers in MyNamespace .

The criticism of both # 1 and # 2 is that they apply to the entire namespace, when you probably only care about defining the members of MyNamespace::MyClass . This is difficult, and he does not communicate intentions well.

A possible alternative # 1 is using-declaration , which includes only the identifier you are interested in:

 #include "MyClass.h" using MyNamespace::MyClass; int MyClass::foo() { ... } 
+3
Jun 01 '16 at 20:57
source share

I would also like to add that if you decide for some reason to implement specialized specialization in the cpp file and just rely on using namespace , you will encounter the following problem:

 // .h file namespace someNameSpace { template<typename T> class Demo { void foo(); }; } // .cpp file using namespace someNameSpace; template<typename T> void Demo<T>::foo(){} // this will produce // error: specialization of 'template<class T> void someNameSpace::Demo<T>::foo()' in different namespace [-fpermissive] template<> void Demo<int>::foo(){} 

Otherwise, if you apply method # 2, it will be fine.

+1
Jun 04 '16 at 12:26
source share

I would like to add another way using using-declaration:

 #include "MyClass.h" using MyNamespace::MyClass; int MyClass::foo() { ... } 

Thus, you cannot repeatedly enter the namespace name if the class has many functions

0
Dec 07 '16 at 6:39
source share



All Articles