如何协调分离标题/源代码与模板的C ++习惯用法? [英] How to reconcile the C++ idiom of separating header/source with templates?

查看:109
本文介绍了如何协调分离标题/源代码与模板的C ++习惯用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在C和C ++中,通常将声明放在源文件中的头文件和定义中,并且保持两者完全分开。然而,就模板而言,这似乎不可能(以任何好的方式),并且众所周知,模板是一个很好的工具。



另外,Boost主要是标题,所以这是一个真正的问题。在C ++中分隔头文件和源代码仍然是一个好主意,或者我应该不太依赖模板吗?

解析方案

实例化模板在编译时是昂贵的,但在运行时是虚拟的。基本上,每次使用新的模板类型时,编译器都必须生成新类型的代码,这就是为什么代码位于头文件中,以便编译器可以稍后访问代码。



将所有代码放在一个.cpp文件中,让编译器只编译一次该代码,这大大加快了编译速度。理论上你可以将所有的代码写入头文件中,它可以正常工作,但编译非常大的项目将会花费很长时间。此外,只要你在任何地方改变一行,你将不得不重建一切。



现在你可能会问,STL和BOOST怎么不那么慢呢?这就是预编译头文件来解决问题的地方。 PCH让编译器只做一次最昂贵的工作。这适用于不会像库一样经常更改的代码,但对于更改很多的代码,其效果完全无效,因为您必须每次重新编译整套预编译头文件。编译器还使用了一些技巧来避免重新编译每个编译单元中的所有模板代码。

还要注意,C ++ 0x将引入显式机制来更好地控制模板实例化。您将能够显式实例化模板,并且最重要的是,可以防止某些编译单元中的实例化。但是,大部分工作已经在大部分编译器的帮助下完成了,因为我们不知道。

因此,根据经验法则,将尽可能多的代码(和包含指令)可能在你的.cpp。如果你不能,那么你不能。



我的建议是:不要模板只为它的模样 。如果您必须进行模板化,请小心并注意您实际上是在编译速度和模板可用性之间进行选择。

I'm wondering a bit about this templating business.

In C and C++ it is very common to put declarations in header files and definitions in source files, and keep the two completely separate. However, this doesn't even seem to be possible (in any great way) when it comes to templates, and as we all know, templates are a great tool.

Also, Boost is mostly headers, so this is a real issue. Is separating headers and source still a good idea in C++, or should I just not rely heavily on templates?

解决方案

Instantiating a template is costly at compile time but virtualy free at runtime. Basically, everytime you use a new template type, the compiler has to generate the code for that new type, that's why the code is in a header, so that the compiler have access to the code later.

Putting all your code in a .cpp lets the compiler compile that code only once which greatly speeds up compilation. You could in theory write all your code in headers, it will work fine, but it will take forever to compile very large projects. Also, as soon as you will change one line anywhere, you will have to rebuild everything.

Now you might ask, how comes the STL and BOOST are not so slow? That's where precompiled headers come to the rescue. PCHs let the compiler do the most costly work only once. This works well with code that won't change often like libraries, but its effect is totally nullified for code that changes a lot as you will have to recompile the whole set of precompiled headers everytime. The compiler also use a couple of tricks to avoid recompiling all template code in every compilation unit.

Also note that C++0x will introduce explicit mechanisms to better control template instantiation. You will be able to explicitly instantiate templates and, most importantly, prevent instanciation in some compilation units. However, most of that work is already being done by most compilers without our knowledge.

So, the rule of thumb is, put as much code (and include directives) as possible in your .cpp. If you can't, well, you can't.

My advice would be: don't template just for the heck of it. If you have to template, be careful and be aware that you are in fact choosing between speed of compilation and usability the template will bring.

这篇关于如何协调分离标题/源代码与模板的C ++习惯用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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