什么时候模板实例化膨胀问题在实践中? [英] When does template instantiation bloat matter in practice?

查看:137
本文介绍了什么时候模板实例化膨胀问题在实践中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在C ++和D中,静态编译的语言和哪个模板元编程是一种流行的技术,对模板实例化的膨胀有一个很大的关注。在我看来,除了非常资源有限的嵌入式系统之外,主要是理论上的关注。在嵌入式空间之外,我还没有听到有人能够证明这在实践中是一个问题的例子。



任何人都可以提供一个例外严重资源有限的嵌入式系统,其中模板实例化膨胀在实践中起重要作用,并且具有可衡量的,实际显着的负面影响

解决方案

C ++中没有什么问题,因为C ++中可以的模板内容的数量受到复杂程度的限制。


然而,在D中,在CTFE(编译时功能评估)之前,我们不得不使用字符串处理的模板。这也是DMD中被压缩的大的错误符号的原因 - 用作模板参数的字符串成为被破坏的符号名称的一部分,并且当实例化具有较长代码段的模板(例如)时,生成的符号大小会惊人地爆炸对象格式。



现在更好了。但总的来说,模板仍然会因为简单的原因造成很大的膨胀 - 它们解析速度更快,而且比C ++更强大,所以人们自然会使用它们(即使在技术上也不需要模板)。我必须承认我是这里的主要罪犯之一(请查看 tools.base ,如果你喜欢,但一定要保持barf包方便 - 文件是90%的模板代码)。


It seems that in C++ and D, languages which are statically compiled and in which template metaprogramming is a popular technique, there is a decent amount of concern about template instantiation bloat. It seems to me like mostly a theoretical concern, except on very resource-constrained embedded systems. Outside of the embedded space, I have yet to hear of an example of someone being able to demonstrate that it was a problem in practice.

Can anyone provide an example outside of severely resource-limited embedded systems of where template instantiation bloat mattered in practice and had measurable, practically significant negative effects?

解决方案

There's little problem in C++, because the amount of template stuff you can do in C++ is limited by their complexity.

In D however ... before CTFE (compile-time function evaluation) existed, we had to use templates for string processing. This is also the reason big mangled symbols are compressed in DMD - the strings used as template arguments become part of the mangled symbol names, and when instantiating templates with longer segments of code (for instance) the resulting symbol sizes would spectacularly explode the object format.

It's better nowadays. But on the whole, templates still cause a lot of bloat for a simple reason - they parse faster and are more powerful than in C++, so people naturally use them a lot more (even in cases that technically wouldn't need templates). I must admit I'm one of the prime offenders here (take a look at tools.base if you like, but be sure to keep a barf bag handy - the file is effectively 90% template code).

这篇关于什么时候模板实例化膨胀问题在实践中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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