Using Ads in Header Files

I was looking for some clarification on the use of ad declarations in header files (I was searching, but could not get the answer I'm looking for). So far I have done my research using them in a non-global area, this is normal, while the namespace directives are bad. I understood this (at least I hope so :)).

So, in my example, I use shared_ptr s, but I need to support older compilers that do not have them in the std:: , but in std::tr1:: for example. Since each class using shared_ptr requires the same rejection of shared_ptr , I would have to put the correct #include directive and use the declaration in each of these header files. So I moved this part to a separate header file, so I only have one file where I need to make changes. The solution for which shared_ptr use is made using the HAS_SHAREDPOINTER preprocessor HAS_SHAREDPOINTER , which is set if the user has a compiler that supports std::shared_ptr .

SharedPtr.h

 #ifndef SHAREDPTR_H_ #define SHAREDPTR_H_ #ifdef HAS_SHAREDPOINTER #include <memory> using std::shared_ptr; #else #include <tr1/memory> using std::tr1::shared_ptr; #endif #endif /* SHAREDPTR_H_ */ 

Now in every header file that uses shared_ptr, I include this header file. For example, in

ModelPar.h

 #ifndef MODELPAR_H_ #define MODELPAR_H_ #include <string> #include <set> #include "SharedPtr.h" class ModelPar { private: std::set<shared_ptr<ModelPar> > connections; ... }; #endif /* MODELPAR_H_ */ 

Now I think I did it wrong, since a user who includes any of my header file (using shared_ptr s) also has the appropriate use of the declaration in his code. And this is bad, because the user does not know about it ... etc. Therefore, I place my usage declarations in the global scope. Or? I’m kind of stuck and confused about how to do it right? Thanks in advance!

+1
file declaration header using
source share
2 answers

Ok, I found the answer "myself." I assume that I did not know that using the declaration in the namespace still works in the namespace subspaces of the same name. Now Bjarne’s words also make more sense that you can’t pollute the global namespace :). Please correct me if I'm still something wrong.

SharedPtr.h:

 #ifndef SHAREDPTR_H_ #define SHAREDPTR_H_ #ifdef HAS_SHAREDPOINTER #include <memory> namespace blub { using std::shared_ptr; } #else #include <tr1/memory> namespace blub { using std::tr1::shared_ptr; } #endif #endif /* SHAREDPTR_H_ */ 

ModelPar.h:

 #ifndef MODELPAR_H_ #define MODELPAR_H_ #include <string> #include <set> #include "SharedPtr.h" namespace blub { class ModelPar { private: std::set<shared_ptr<ModelPar> > connections; ... }; } #endif /* MODELPAR_H_ */ 
0
source share

I personally do not see any benefit from the "use" in any header. Always .

Not only is it very difficult to regroup, as compiler errors will become useless if you remove the include chain header. Getting 300+ errors of skipped ads, undefined types, etc .; not really my definition of "funtimes".

Of course, you can use preprocessor magic to do this regardless of name collisions. But why? If you have namespace conflicts, you have problems with your approach, a workaround that is in this way as good as turning off a fire alarm, and claiming that the fire no longer works.

As an added bonus, it also hides the pedigree of your class a la "Did I use FooClass from the namespace X? Or was it from Y?".

0
source share

All Articles