如何使用C ++模板减少编译时间 [英] How to reduce compile time with C++ templates

查看:653
本文介绍了如何使用C ++模板减少编译时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的C ++应用程序的一部分从使用旧的C类型数组更改为模板化的C ++容器类。请参阅这个问题。虽然解决方案工作得很好,我对模板化代码的每次细微更改都会导致大量的重新编译,从而极大地减慢了构建时间。有没有办法从模板代码中取出模板代码并返回一个cpp文件,以便较小的实现更改不会导致重大的重建?

I'm in the process of changing part of my C++ app from using an older C type array to a templated C++ container class. See this question for details. While the solution is working very well, each minor change I make to the templated code causes a very large amount of recompilation to take place, and hence drastically slows build time. Is there any way of getting template code out of the header and back into a cpp file, so that minor implementation changes don't cause major rebuilds?

推荐答案

我认为一般规则适用。尝试减少代码部分之间的耦合。

I think the general rules apply. Try to reduce coupling between parts of the code. Break up too large template headers into smaller groups of functions used together, so the whole thing won't have to be included in each and every source file.

此外,尝试将所有的模板标题分解成一起使用的更小的函数组,因此整个事件不必包含在每个源文件中。以使标题能够快速地进入稳定状态,可能会针对较小的测试程序进行测试,因此当集成到更大的程序中时,它们不需要更改(太多)。

Also, try to get the headers into a stable state fast, perhaps testing them out against a smaller test program, so they wouldn't need changing (too much) when integrated into a larger program.

(与任何优化一样,在处理模板时优化编译器的速度可能不太值得,而不是找到一个算法优化,首先大大减少工作负载。)

(As with any optimization, it might be less worth to optimize for the compiler's speed when dealing with templates, rather than finding an "algorithmic" optimization that reduces the work-load drastically in the first place.)

这篇关于如何使用C ++模板减少编译时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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