在头文件中使用声明 [英] using declaration in header files
问题描述
我一直在寻找头文件中使用 using-declarations 的一些说明(我正在四处搜索,但无法得到我正在寻找的答案)。我的研究结论迄今为止,在非全局范围内使用它们是可以的,而命名空间指令是不好的。我知道(至少我希望如此:))。
所以在我的例子中我使用 shared_ptr
s,但我需要支持在 std ::
命名空间中没有它们但在 std :: tr1 :: $ c $中没有的旧编译器例如。因为每个使用
shared_ptr
的类都需要相同的 shared_ptr
defing,所以我必须将正确的 #include
指令并在每个头文件中使用声明。所以我将这部分移到了一个单独的头文件中,所以我只有一个文件需要进行更改。通过预处理指令 HAS_SHAREDPOINTER
来决定使用哪个 shared_ptr
,如果用户有编译器支持 std :: shared_ptr
。
$ b SharedPtr.h :
#ifndef SHAREDPTR_H_
#define SHAREDPTR_H_
#ifdef HAS_SHAREDPOINTER
#include<存储器GT&;
使用std :: shared_ptr;
#else
#include< tr1 / memory>
使用std :: tr1 :: shared_ptr;
#endif
#endif / * SHAREDPTR_H_ * /
现在在使用shared_ptr的每个头文件中,都包含这个头文件。例如在
ModelPar.h 中:
#ifndef MODELPAR_H_
#define MODELPAR_H_
#include< string>
#include< set>
#includeSharedPtr.h
$ b $ class ModelPar {
private:
std :: set< shared_ptr< ModelPar> >连接;
...
};
#endif / * MODELPAR_H_ * /
现在我想我这是错误的,因为包含我的任何头文件的用户(使用 shared_ptr
s)在他的代码中也有对应的使用声明 。这是坏事,因为用户没有意识到这一点......所以我把我的使用声明放在全局范围内。要么?我有点困惑和困惑如何正确地做到这一点?提前致谢!
好的,我自己找到了答案。我想我没有意识到命名空间中的using声明在名称空间相同的名称空间下仍然有效。现在Bjarne的话也更有意义,人们不应该污染全球命名空间:)。如果我仍然做错了,请纠正我。
SharedPtr.h:
#ifndef SHAREDPTR_H_
#define SHAREDPTR_H_
#ifdef HAS_SHAREDPOINTER
#include< memory>
namespace blub {
使用std :: shared_ptr;
}
#else
#include< tr1 / memory>
namespace blub {
使用std :: tr1 :: shared_ptr;
}
#endif
#endif / * SHAREDPTR_H_ * /
ModelPar.h:
#ifndef MODELPAR_H_
#define MODELPAR_H_
#include< string>
#include< set>
#includeSharedPtr.h
namespace blub {
class ModelPar {
private:
std ::设置< shared_ptr的< ModelPar> >连接;
...
};
}
#endif / * MODELPAR_H_ * /
I've been looking for some clarification for the usage of the using-declarations in header files (I was searching around but couldn't quite get the answer I'm looking for). What I my research concluded so far is, using them in the non-global scope is ok, while namespace directives are bad. That I understood (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::
namespace but in std::tr1::
for example. Since every class using the shared_ptr
needs the same shared_ptr
defition, I would have to put the correct #include
directive and using declaration in each of these header files. So I moved this part into a seperate header file so I have only one single file where I need to make changes. The decision on which shared_ptr
to use, is made via a preprocessor directive 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 the 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 the way I did it is wrong, since the user that includes any of my header file (using the shared_ptr
s) also has the corresponding using declaration in his code. And that's the bad thing, as the user is not aware of this... etc. So I put my using declarations in the global scope. Or? I'm kind of stuck and confused on how to do this properly? Thanks in advance!
Ok I found "the" answer myself. I guess that I was not aware that the using declaration in a namespace is still valid in below namespace scopes with the same name. Now Bjarne's words also makes more sense, that one should not pollute the global namespace :). Please correct me though, if I'm still doing 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_ */
这篇关于在头文件中使用声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!