模板如何工作,它们总是内联吗? [英] how does templates work, are they always inlined?

查看:310
本文介绍了模板如何工作,它们总是内联吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以理解它如何工作,如果他们内联。但如果他们不是,它是如何工作的?

I can understand how it work if they are inlined. But if they are not, how does it work? does all object files get their own copy of for example the function template anyway?

推荐答案

模板将被内联的标准意义 inline ,这与单一定义规则比与实际代码内联更相关。也就是说,如果模板函数在多个翻译单元中被定义,链接器将不会抱怨,它将只挑选一个(当心:随机一个,当前编译器不提出,如果你在不同的翻译单元中提供不同的模板定义!

Templates will be inlined in the standard meaning of inline, which is more related to the One Definition Rule than to actual code inlining. That is, the linker will not complain if the template functions are defined in more than one translation unit, it will just pick one (beware: random one, current compilers do not complain if you provide different definitions of the template in different translation units!) and leave that in the final binary.

现在,和所有其他 inline 函数一样,编译器可以决定实际上避免函数调用和内联函数在调用的地方,或者它可能确定它不是一个好主意(大功能,一些编译器不内联函数与嵌套循环是一个好主意。 。无论什么原因),然后它不会执行实际的代码内联。

Now, as with all other inline functions, the compiler can decide that it is a good idea to actually avoid the function call and inline the function at the place of call, or it might determine that it is not such a good idea (big function, some compilers don't inline functions with nested loops... whatever the reason) and then it will not perform the actual code inlining.

这篇关于模板如何工作,它们总是内联吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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