将声明和实现压缩为HPP文件 [英] Condensing Declaration and Implementation into an HPP file

查看:86
本文介绍了将声明和实现压缩为HPP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些有关在C ++中保留标头的需求/适用性/实用性的文章,但是我似乎找不到任何可靠的理由来说明何时/何时不应该执行上述操作.我知道boost不需要使用相关的.cpp文件就可以使用.hpp文件向最终用户提供模板功能,并且这种想法部分源于该代码的浏览.似乎这是一种传递单个文件模块(例如说新的Wt或Qt小部件)的便捷方法(按照.h约定仍然坚持使用一个类).

I've read a few of the articles about the need / applicability / practicality of keeping headers around in C++ but I can't seem to find anywhere a solid reason why / when the above should or should not be done. I'm aware that boost uses .hpp files to deliver template functions to end users without the need for an associated .cpp file, and this thought is partially sourced off browsing through that code. It seems like this would be a convenient way to deliver single file modules of say a new Wt or Qt widget (still sticking to the one class per .h convention).

但是,如果有人可以访问该实现(例如在OSS的上下文中),则没有任何负面的技术实现,可以给某人一个带有头声明和实现的.hpp文件.从编译器的/链接器的角度来看,它对实例是否有负面影响?

However are there any negative technical implementations for giving somebody a single .hpp file with both the header declaration and implementation assuming you have no problem with them having access to the implementation (say in the context of OSS). Does it for instances have any negative implications from the compiler's / linker's perspective?

对此表示任何意见或看法.

Any opinions or perspectives on this would be appreciated.

推荐答案

意识到,boost使用.hpp文件向最终用户提供模板功能,而无需相关的.cpp文件

'm aware that boost uses .hpp files to deliver template functions to end users without the need for an associated .cpp file

动词错误:不是没有需要",而是没有 能力 ".

Wrong verb: it’s not "without the need", it’s "without the ability".

如果Boost可以,他们会将其库分为标头和实现文件.实际上,他们会尽可能这样做.

If Boost could, they would separate their libraries into headers and implementation files. In fact, they do so where ever possible.

完全分开的原因很简单:纯标题项目的编译时间大大增加,因为每次重新编译应用程序最细小的部分时,都必须读取,解析和编译关联的标题文件

The reason for a clean separation is simple: compilation time for header-only projects increases tremendously because associated header files have to be read, parsed and compiled every time you recompile the tiniest part of your application.

仅当您碰巧重新编译该特定目标文件时,才需要编译实现文件.

Implementation files only need to be compiled if you happen to recompile that particular object file.

大型C和/或C ++项目花费小时进行编译.这些使用将头文件和目标文件完全分开.如果他们只使用头文件,我敢打赌,编译时间将以为单位,而不是以小时为单位.

Large C and/or C++ projects take hours to compile. And these use a clean separation into header and object files. If they would only use header files, I’m betting the compilation time would be measured in days instead of hours.

但是对于许多Boost的库来说,事实是模板定义可能不位于声明之外的单独的编译单元中,所以这根本不可能.

But for many of Boost’s libraries, the fact is that template definitions may not reside in a separate compilation unit than their declarations so this is simply not possible.

这篇关于将声明和实现压缩为HPP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆