Should I include the header file in the namespace?

I am currently working on a c-framework, and I want to embed a C ++ package into it. However, there are many name conflicts. So I decided to add a namespace to the C ++ source. Now the question is, should I move #include "header.h" to the {} block namespace? I just took some time to figure out the error that was caused by the following codes.

C ++ source

In ah

#include <unistd.h> struct File { void func(int fd); }; 

In a.cpp

 #include "ah" void File::func(int fd) { ::close( fd ); } 

And I added a namespace like this

New ah

 namespace MyAddedNameSpace { #include <unistd.h> struct File { void func(int fd); }; } 

New a.cpp file

 #include "ah" namespace MyAddedNameSpace { void File::func(int fd) { ::close( fd ); } } 

And the compiler complains that :: close () was not declared.

The reason I put the #include directive inside a namespace block is because the imported C ++ package also uses the #ifndef flag to include header files as follows. Therefore, I believe that a simple way is to put all the codes in a namespace block {}

 #ifndef #include <header1.h> #include <header2.h> ... #else #include <header3.h> #include <header4.h> ... #endif 

Now I solved this problem by adding an extra line to the cpp file

 #include <unistd.h> //new added line #include "ah" namespace MyNameSpace { void File::func(int fd) { ::close( fd ); } } 

But I'm not satisfied with this solution, since the unistd.h header was already included in ah, but inside the MyAddedNameSpace namespace, or should I add the MyNameSpace prefix to all function calls, where the compiler complains that such a function is not declared?

Thanks for the answer.

+4
source share
2 answers

It is usually sufficient to place the using namespace directive in the .cpp file. For instance:

 using namespace MyAddedNameSpace; void File::func(int fd) { close( fd ); } 

Hope this helps.

0
source

SHOULD NOT place #include directives inside your own namespace. What you did is place all the contents of unistd.h inside your namespace; in such a way that earlier (and should remain!) ::close() now declared as MyAddedNameSpace::close() . This is NOT what you want.

Your "decision" to add the line #include <unistd.h> at the top of your .cpp file fixes the problem, but only for that .cpp file. What you did includes the library title correctly (without your namespace), and then when the title (ah) is included, it again #include <unistd.h> (this time in your namespace), but this time The included guards in this file are prohibited from re-processing it. So, for this .cpp file, everything is fine with you, but any other file that executes #include <ah> will have the same problem as yours.

There may be some rare case where you have a good reason to use #include in your own namespace, but you will most likely include one of your own headers (or some other file) - NOT - a header libraries! - and even then this is probably not an ideal solution.

In ah

 #include <unistd.h> namespace MyAddedNameSpace { struct File { void func(int fd); }; } 

In a.cpp

 #include "ah" namespace MyAddedNameSpace { void File::func(int fd) { ::close( fd ); } } 
+19
source

Source: https://habr.com/ru/post/1402162/


All Articles