内联的标准是什么 [英] What is the criterion to be inline

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

问题描述

由于内联请求不是强制性的,因此编译器如何计算应内联的函数?我应该内联什么样的功能?

How does compiler calculate which function should be inline since inline request is not obligatory? What kind of functions should I make inline?

其他问题.我可以拨打电话吗?

Extra question. Can I make an inline call?

void func{} // normal (not-inline) function

int main()
{
  func();           // normal call
  // inline func(); // inline call
  return 0;
}

推荐答案

由于内联请求不是强制性的,因此编译器如何计算应内联的函数?

How does compiler calculate which function should be inline since inline request is not obligatory?

  • (来自 wiki )在各种版本的C和C ++编程语言中,内联函数是已要求编译器执行内联扩展的函数.换句话说,程序员已要求编译器在函数调用的每个位置插入函数的完整主体,而不是生成代码以在定义的那个位置调用函数.编译器没有义务遵守此请求.

    • (from wiki) In various versions of the C and C++ programming languages, an inline function is a function upon which the compiler has been requested to perform inline expansion. In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.

      在C ++中,可以使用__forceinline关键字允许程序员强制编译器内联该函数,但是不加选择地使用__forceinline可能会导致代码更大(可执行文件blo肿),性能提高到最低或没有,并且在某些情况下甚至会降低性能.

      In C++, you can use __forceinline keyword allow the programmer to force the compiler to inline the function, but indiscriminate use of __forceinline can result in larger code (bloated executable file), minimal or no performance gain, and in some cases even a loss in performance.

      我应该内联什么样的功能?

      What kind of functions should I make inline?

      • 小功能.在头文件中定义小的(如在一个衬里中)函数是一个好主意,因为它为编译器提供了更多信息,可在优化代码时使用.这也增加了编译时间.

        • Small functions. It's a good idea to define small (as in one liner) functions in the header file as it gives the compiler more information to work with while optimizing your code. It also increases compilation time.

          查看何时应编写关键字内联"以获取功能/方法?以获取更多信息.

          我可以拨打电话吗?

          Can I make an inline call?

          • 您不能进行内联调用,因为inline仅可以在函数声明中使用,而不能在调用中使用.您只需要将预定义的内联函数作为普通函数调用即可.
            • You cannot make an inline call as inline can only used in function declaration, not calling. You just need to call a pre-defined inline function as normal functions.
            • 这篇关于内联的标准是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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