兼容c ++ 11的编译器是否总是忽略内联提示? [英] Do c++11-compatible compilers always ignore inline hints?

查看:95
本文介绍了兼容c ++ 11的编译器是否总是忽略内联提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

旧答案我应该何时为功能/方法写关键字内联?上面写着:


据说内联向编译器暗示您认为应该内联函数。 1998年可能确实如此,但是十年后,编译器不需要这些提示。更不用说人类在优化代码方面通常是错误的,因此大多数编译器都会完全忽略提示。


此答案


  1. 做兼容现代c ++ 11的编译器总是 em>忽略用户指定的内联提示,并且仅自动执行此操作吗?

  2. 执行内联提示是否仅保留提供向后兼容性?

  3. 如果不是 1。,那么这个答案不正确吗?


解决方案



  1. 现代c ++ 11兼容的编译器总是忽略用户指定的内联提示并执行此操作


C ++ 11在这里无关紧要,C ++ 11标准没有更改 inline 的语义,并且编译器的优化在很大程度上与所编译的语言版本无关。



  1. 内联提示是否仅保留提供向后兼容性?


否,内联不是提示,编译器不会忽略 内联因为如果这样做,您将获得多个定义错误。编译器赋予 inline 关键字的含义不是您似乎理解的含义。



如果编译器看不到函数定义,则无法内联它(链接时优化对此进行了更改,但是使用LTO不能

您应该阅读 inline 表示为此函数定义在此文件中内联而不是应内联对此函数的调用。



因此 inline 关键字对于允许编译器查看多个文件中的函数定义很有用,这意味着可能使其可以通过内联来优化调用。并不一定比同一个翻译单元中定义的任何其他函数更容易内联。



对于从多个定义了函数的翻译单元调用的函数在标头中并使其内联是编译器内联的必要条件,但不足(因为



这与向后兼容性无关,今天与2009年一样。


Reading an old answer on When should I write the keyword 'inline' for a function/method? that says:

It is said that inline hints to the compiler that you think the function should be inlined. That may have been true in 1998, but a decade later the compiler needs no such hints. Not to mention humans are usually wrong when it comes to optimizing code, so most compilers flat out ignore the 'hint'.

This answer was posted in 2009 so I want to figure it finally out:

  1. Do modern c++11-compatible compilers always ignore inline hints specified by user and do this only automatically?
  2. Do inline hints only stay to provide backward compatibility?
  3. If not 1. so this answer is incorrect?

解决方案

  1. Do modern c++11-compatible compilers always ignore inline hints specified by user and do this only automatically?

C++11 is irrelevant here, the C++11 standard did not change the semantics of inline and compiler optimisations are largely independent of the language version being compiled.

  1. Do inline hints only stay to provide backward compatibility?

No, inline is not a hint, and the compiler doesn't "ignore" inline because if it did that you'd get multiple definition errors. The meaning the compiler gives to the inline keyword is not the meaning you seem to understand. It's not a hint.

If the compiler can't see the function definition it can't inline it (Link-Time Optimization changes that, but use of LTO is not very widespread yet and most libraries do not ship with LTO-enabled binaries that would allow link-time inlining).

You should read inline as "this function definition appears inline in this file" not "calls to this function should be inlined".

So the inline keyword is useful for allowing the compiler to see function definitions in multiple files, which means it is possible for it to optimise the calls away by inlining them. That doesn't necessarily make it more likely to be inlined than any other function defined in the same translation unit.

For functions that are called from multiple translation units defining the functions in headers and making them inline is necessary condition for the compiler to inline them, but it is not sufficient (because the compiler bases its inlining decisions on other conditions).

This has nothing to do with backwards compatibility, it is just as true today as in 2009.

这篇关于兼容c ++ 11的编译器是否总是忽略内联提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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