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!
file declaration header using
steve
source share