或者也许是我。 ;-) - Phlip http://www.greencheese.org/ZeekLand < - 不是博客!!! 哦,还有一个事情。过多的操作码内联可能会使调用 函数溢出CPU缓存,这会降低程序速度。 而不是读取机器语言输出和猜测关于 优化,测量你的程序,看看它的速度慢。它可能不是这些模板的价值!只优化速度慢得多的部件。这个 策略优化了程序员的表现。 - Phlip http://www.greencheese.org/ZeekLand < - 不是博客!!! Hi:What rules govern the inlining of templated functions and templatedclass methods?It has always been my understanding that both templated functions andtemplated class methods were always expanded inline. Recently, Ireplaced an explicitly written function with one implemented usingtemplates (and partial-template specialisation), in the belief that thethe latter would be entirely inlined-away by the compiler, leaving thesame machine code as the explicitly written function and nofunction-call overhead. However, analysis of the machine code producedby my compiler (Visual C++ 7.1) indicates that the templated functionsare not entirely being removed. In other words, there are stillfunction calls. By forcing inline expansion (using the non-standardsconformant __forceinline keyword), I can remove the function calls andmake my templated implementation produce exactly the same machine codeas the explicitly written function (and, more importantly, the sameperformance). MingW is also not inlining the templated code.So, my questions are:1. Why aren''t my templated functions and templated class methods callsbeing removed (i.e., optimised away) by inlining?2. Are templated functions and templated class methods supposed to beinlined by the compiler?3. Does the inline keyword have any impact when used with templatedfunctions and templated class methods?4. Is there something fundamentally wrong with my understanding andanalysis of all of this?Long e-mail, but I would appreciate any clarification!Thanks,El 解决方案Firstly, inlining is only vaguely defined by the standard and always only asuggestion to the compiler. The compiler is free to inline functionswithout the ''inline'' keyword and to not inline them even with it. Peoplealso say that it''s a ''quality of implementation'' issue.Secondly, what ''inline'' guarantees is that it prevents a duplicatedefinition from creating an error, but that''s the only thing guaranteed.Uli--FAQ: http://ma.rtij.nl/acllc-c++.FAQ.htmlOr maybe me. ;-)--Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!Oh, one more thing. Excessive opcode inlining might make the callingfunction overflow your CPU cache, and this will slow down your program.Instead of reading the machine language output and guessing aboutoptimization, measure your program and see where it''s slow. It might not bethese templates! Only optimize the parts that are measurably slow. Thisstrategy optimizes programmer performance.--Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!! 这篇关于模板和内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-15 20:30