简体   繁体   English

我应该#include我的图书馆名称空间吗?

[英]Should I #include in my library's namespace?

I have a library that lives in its own namespace. 我有一个位于其自己的命名空间中的库。 Every file is wrapped by this namespace. 每个文件都由该命名空间包装。 When I use the standard library, should I include it into the global namespace or the one of my library? 使用标准库时,应该将其包括在全局名称空间中还是其中一个库中? Moreover, what about library intern includes. 此外,图书馆实习生包括什么。 Should they go inside or outside the namespace? 他们应该进入名称空间内部还是外部?

#include <vector> // This allows reuse

namespace library {
    #include <vector> // This would not leak anything
    #include "cart.h"
    using namespace std;

    class Train {
        vector<cart> carts;
    };
}

Including into the global namespace would make sense when the user needs vectors, too, so that the compiler doesn't need to integrate two versions of the standard library. 当用户也需要向量时,将其包含在全局名称空间中也是有意义的,因此编译器不需要集成标准库的两个版本。 On the other hand, including into the library's namespace makes sure that nothing leaks outside. 另一方面,将其包含到库的名称空间中可确保没有任何外部泄漏。

Applied 'common sense' suggests that you can't reliably include C++ standard headers inside your own namespace. 应用“常识”建议您不能在自己的名称空间中可靠地包含C ++标准标头。 The standard library code is meant to be in the std namespace, not the library::std namespace. 标准库代码应位于std名称空间中,而不应位于library::std名称空间中。 If there are any non-templated functions, the system library will provide std::non_templated_function and not library::std::non_templated_function , so your code won't link. 如果有任何非模板函数,系统库将提供std::non_templated_function而不是library::std::non_templated_function ,因此您的代码将不会链接。 If everything is a template, you may get away with it, but it is (unnecessarily) risky at best so don't do it. 如果所有内容都是模板,则可能会避开它,但它充其量是(不必要地)有风险的,因此不要这样做。

Further (as noted by TC in a comment ), the C++11 standard (ISO/IEC 14882:2011) standard explicitly says in §17.6.2.2 Headers [using.headers] : 此外(如TC评论中指出的那样),C ++ 11标准(ISO / IEC 14882:2011)标准在§17.6.2.2标头[using.headers]中明确指出:

¶3 A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header. ¶3翻译单元应仅在任何外部声明或定义之外包括头,并且应在该翻译单元中首次引用该头中声明的任何实体之前按词法包括头。

This refers to a header from the C++ standard library, of course; 当然,这是指C ++标准库中的标头。 it makes no such proscription for your own headers. 它不会为您自己的标题提供这样的禁止。

In theory, your library's internal headers could be included inside your library namespace if you want (but they shouldn't be — see below): 从理论上讲,您可以根据需要将库的内部标头包含在library名称空间中(但不应包含在标头中):

namespace library {
    #include "library/internal-header.h"
    …other declarations or definitions…
}

However (as Matt McNabb noted in a comment ), doing so would mean that the internal header could not include any new standard headers, and yet it is quite plausible that an internal header would need to use some (extra) standard headers. 但是,(如Matt McNabb评论中指出的那样),这样做将意味着内部标头不能包含任何新的标准标头,但是内部标头需要使用一些(额外的)标准标头是很合理的。

You might care to think about including them inside your own internal sub-namespace (for example, library::internal ). 您可能会考虑将它们包含在自己的内部子命名空间中(例如, library::internal )。 If your internal headers do not contain their own namespace library { … } block, ensure you always include them inside the scope of your namespace library { … } brackets in the headers that include them, but note that the internal headers are no longer completely standalone. 如果内部标头不包含自己的namespace library { … }块,请确保始终将它们包含在包含它们的标头中的名称空间library { … }括号的范围内,但请注意,内部标头不再完全独立。

Alternatively, and much more reliably, your library's internal headers could should be made standalone, with their contents defined inside your library namespace (since namespaces are extensible). 另外,和更加可靠,图书馆的内部接头可能应作出独立的,它们的内容您的内部定义library命名空间(因为名称空间是可扩展)。

#include "library/internal-header.h"
namespace library {
    …other declarations or definitions…
}

where "library/internal-header.h" would contain: 其中"library/internal-header.h"将包含:

namespace library {
    namespace internal {
        …internal declarations or definitions…
    }
}

where the namespace internal { and matching } are optional — and you might want a using namespace library::internal; namespace internal {和匹配}是可选的—您可能需要using namespace library::internal; directive as well. 指令。

Either mechanism could be made to work. 两种机制都可以起作用。 On the whole, though, standalone headers are much the better (I agree with πάντα ῥεῖ and his comment and Matt McNabb). 总体而言,独立标头要好得多(我同意πάνταῥεῖ及其评论和Matt McNabb的观点 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何将其包含在我的静态库中? - How should I include in my static library? 我应该在名称空间中包含头文件吗? - Should I include header file within a namespace? 我应该将DECLSPEC放在命名空间中的哪个位置? - Where should I put my DECLSPEC for a namespace? 我应该在源文件中包含包含命名空间定义的 header 吗? - Should I include the header containing the definition of a namespace in source files? 内部库类型是否应该通过我的库的界面可见? - Should internal library types be visible through my library's interface? 如何公开库的枚举,以便我的代码不必键入整个命名空间即可使用该枚举? - How do I expose a library's enum, such that my code doesn't have to type out the entire namespace to use that enum? 我应该使用单个头文件来包含所有静态库头文件吗? - Should I use a single header to include all static library headers? 我应该在我的库中使用线程吗? - Should I use thread in my library? 无法使用名称空间,并且不能在我的.h文件中包含标准C ++库 - cannot use namespace and cannot include standard C++ library in my .h files 我应该包含外部库的 src 代码还是从外部库构建的静态库? - Should I include src code of external library or a static library built from an external library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM