模板和内联 [英] Templates and Inlining

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

问题描述

嗨:


什么规则管理模板化函数的内联和模板化的

类方法?


我的理解一直是模板化函数和模板化方法总是内联扩展。最近,我用
替换了一个明确编写的函数,其中一个使用

模板(和部分模板特化)实现,相信

后者将完全由编译器内联,保留

与明确编写的函数相同的机器代码,并且没有

函数调用开销。但是,由我的编译器(Visual C ++ 7.1)分析我生成的机器代码

表明模板化函数

并没有被完全删除。换句话说,还有

函数调用。通过强制内联扩展(使用非标准符号_ b $ b符合__forceinline关键字),我可以删除函数调用和

使我的模板化实现产生完全相同的机器代码

作为明确编写的函数(更重要的是,相同的

性能)。 MingW也没有列出模板代码。


所以,我的问题是:


1.为什么我不是模板化的函数模板化类方法调用

通过内联删除(即优化掉)?

2.模板函数和模板类方法应该是

由编译器内联?

3.当与模板化的

函数和模板化类方法一起使用时,内联关键字是否有任何影响?

4我的理解是否存在根本性的错误?

分析所有这些?


长电子邮件,但我要感谢任何澄清! />

谢谢,

El

解决方案

Elpoca写道:< blockquote class =post_quotes>什么规则管理模板化函数和模板化类方法的内联?

我一直都理解模板函数和模糊函数模板化类方法总是内联扩展。最近,我用一个使用
模板(和部分模板特化)实现的函数替换了一个明确编写的函数,相信后者将被编译器完全内联掉, [...]


我认为模板专业化不是隐式内联的,尽管

对于规则适用的类,每个成员函数在体内定义是隐式内联的。

[..]留下与明确编写的函数相同的机器代码而没有
函数调用开销。但是,我的编译器(Visual C ++ 7.1)对生成的机器代码的分析表明,模板化的函数并没有被完全删除。换句话说,还有
函数调用。通过强制内联扩展(使用非标准符合__forceinline关键字),我可以删除函数调用,并使我的模板化实现生成与明确编写的函数完全相同的机器代码(而且,更重要的是,相同的表现)。 MingW也没有列出模板化的代码。

所以,我的问题是:

1.为什么我的模板化函数和模板类方法调用不是
通过内联删除(即优化掉)?
2.模板化函数和模板化类方法是否应该由编译器内联?
3.内联关键字是否有任何影响与模板化的
函数和模板化的类方法一起使用?
4.我对这一切的理解和分析是否存在根本性的错误?




首先,内联只是模糊地定义了标准,并且始终只对编译器提出建议。编译器可以自由内联函数

而不使用''inline''关键字,并且即使使用它也不能内联它们。人们

也说这是''实施质量''的问题。


其次,'内联'保证是它可以防止一个重复的

定义来创建错误,但这是唯一保证的。


Uli


-

常见问题: http:/ /ma.rtij.nl/acllc-c++.FAQ.html


Elpoca写道:

什么规则管理模板化函数和模板化类方法的内联?

我一直认为模板化函数和模板化类方法总是内联扩展。


关键字''inline''有两个隐含的表现形式。函数定义

在一个类和模板函数中,对它们有一个隐含的''inline''。


''inline''的含义是并不是函数的操作码会扩展

inline。这往往是副作用。意思是该函数可以打破单一定义规则,并且可以出现在许多翻译单元中(.cpp

文件)。函数中所有东西的类型必须始终相同。


该规则允许编译器积极优化函数的

操作码,每次一次对象文件。因此通常会产生内联操作码。

但是,编译器也允许使用未声明的内联函数

''inline''。


编译器可以做任何它想要优化的事情,只要结果

起作用就好像一样。它遵循了标准关于

操作码的说明。

所以,我的问题是:

1.为什么不是我的模板化函数和模板化类方法调用通过内联被删除(即优化掉)?


因为这是针对那些编译器的特定实现。它们可能都是b
$ 2.
2.模板化的函数和模板类方法是否应该由编译器内联?


No.

3.当与模板化的
函数和模板化类方法一起使用时,inline关键字是否会产生任何影响?


No.

4.我对这一切的理解和分析是否存在根本性的错误?



或者也许是我。 ;-)


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!


Elpoca写道:

什么规则管理模板化函数和模板化类方法的内联?




哦,还有一个事情。过多的操作码内联可能会使调用

函数溢出CPU缓存,这会降低程序速度。


而不是读取机器语言输出和猜测关于

优化,测量你的程序,看看它的速度慢。它可能不是这些模板的价值!只优化速度慢得多的部件。这个

策略优化了程序员的表现。


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!


Hi:

What rules govern the inlining of templated functions and templated
class methods?

It has always been my understanding that both templated functions and
templated class methods were always expanded inline. Recently, I
replaced an explicitly written function with one implemented using
templates (and partial-template specialisation), in the belief that the
the latter would be entirely inlined-away by the compiler, leaving the
same machine code as the explicitly written function and no
function-call overhead. However, analysis of the machine code produced
by my compiler (Visual C++ 7.1) indicates that the templated functions
are not entirely being removed. In other words, there are still
function calls. By forcing inline expansion (using the non-standards
conformant __forceinline keyword), I can remove the function calls and
make my templated implementation produce exactly the same machine code
as the explicitly written function (and, more importantly, the same
performance). MingW is also not inlining the templated code.

So, my questions are:

1. Why aren''t my templated functions and templated class methods calls
being removed (i.e., optimised away) by inlining?
2. Are templated functions and templated class methods supposed to be
inlined by the compiler?
3. Does the inline keyword have any impact when used with templated
functions and templated class methods?
4. Is there something fundamentally wrong with my understanding and
analysis of all of this?

Long e-mail, but I would appreciate any clarification!

Thanks,
El

解决方案

Elpoca wrote:

What rules govern the inlining of templated functions and templated
class methods?

It has always been my understanding that both templated functions and
templated class methods were always expanded inline. Recently, I
replaced an explicitly written function with one implemented using
templates (and partial-template specialisation), in the belief that the
the latter would be entirely inlined-away by the compiler, [..]
I believe that a template specialisation is not implicitly inline, although
for classes the rule applies that every memberfunction defined in the body
of the class definition is implicitly inline.
[..] leaving the
same machine code as the explicitly written function and no
function-call overhead. However, analysis of the machine code produced
by my compiler (Visual C++ 7.1) indicates that the templated functions
are not entirely being removed. In other words, there are still
function calls. By forcing inline expansion (using the non-standards
conformant __forceinline keyword), I can remove the function calls and
make my templated implementation produce exactly the same machine code
as the explicitly written function (and, more importantly, the same
performance). MingW is also not inlining the templated code.

So, my questions are:

1. Why aren''t my templated functions and templated class methods calls
being removed (i.e., optimised away) by inlining?
2. Are templated functions and templated class methods supposed to be
inlined by the compiler?
3. Does the inline keyword have any impact when used with templated
functions and templated class methods?
4. Is there something fundamentally wrong with my understanding and
analysis of all of this?



Firstly, inlining is only vaguely defined by the standard and always only a
suggestion to the compiler. The compiler is free to inline functions
without the ''inline'' keyword and to not inline them even with it. People
also say that it''s a ''quality of implementation'' issue.

Secondly, what ''inline'' guarantees is that it prevents a duplicate
definition from creating an error, but that''s the only thing guaranteed.

Uli

--
FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html


Elpoca wrote:

What rules govern the inlining of templated functions and templated
class methods?

It has always been my understanding that both templated functions and
templated class methods were always expanded inline.
The keyword ''inline'' has two implicit manifestations. Functions defined
inside a class, and template functions, have an implicit ''inline'' on them.

The meaning of ''inline'' is not that the function''s opcodes will expand
inline. That is often a side-effect. The meaning is the function can break
the One Definition Rule, and can appear in many translation units (.cpp
files). The type of everything in the function must always be the same.

That rule permits the compiler to aggressively optimize the function''s
opcodes, once per object file. So in-line opcodes are often the result.
However, the compiler is also allowed to in-line functions not declared
''inline''.

The compiler may do anything it likes to optimize, so long as the result
functions "as if" it had followed the Standard''s instructions regarding
opcodes.
So, my questions are:

1. Why aren''t my templated functions and templated class methods calls
being removed (i.e., optimised away) by inlining?
Because that''s implementation specific to those compilers. They probably all
punted.
2. Are templated functions and templated class methods supposed to be
inlined by the compiler?
No.
3. Does the inline keyword have any impact when used with templated
functions and templated class methods?
No.
4. Is there something fundamentally wrong with my understanding and
analysis of all of this?



Or maybe me. ;-)

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


Elpoca wrote:

What rules govern the inlining of templated functions and templated
class methods?



Oh, one more thing. Excessive opcode inlining might make the calling
function overflow your CPU cache, and this will slow down your program.

Instead of reading the machine language output and guessing about
optimization, measure your program and see where it''s slow. It might not be
these templates! Only optimize the parts that are measurably slow. This
strategy optimizes programmer performance.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


这篇关于模板和内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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