如何在调用位置替换Inline函数代码? [英] how Inline function code is replaced in calling place?

查看:158
本文介绍了如何在调用位置替换Inline函数代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何内联函数调用被内联代码替换。
i在某些书中读取,说编译器可能将内联函数视为正常函数。

I want to know how inline function call is replaced by the inline code. i read in some book saying compiler may treat the inline function as normal function.

可以自己解释内联函数的工作原理。

can any own explain how inline function works.

推荐答案

从C ++常见问题:


当编译器内联展开函数调用时,函数的代码
会插入调用者的代码stream(概念上类似于
使用#define宏发生的情况)。这可以,根据zillion
其他的东西,提高性能,因为优化程序可以
过程整合被调用的代码 - 优化被调用的代码到
调用者。

When the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream (conceptually similar to what happens with a #define macro). This can, depending on a zillion other things, improve performance, because the optimizer can procedurally integrate the called code — optimize the called code into the caller.

有几种方法可以指定一个函数是内联的,一些
涉及inline关键字,另一些不是。不管你如何
指定一个函数作为内联,它是一个请求编译器是
允许忽略:它可能内联展开一些,全部或没有
调用到内联函数。 (不要害怕,如果这似乎
绝望模糊上面的灵活性实际上是一个巨大的
优势:它让编译器处理大型函数不同于
小型,加上它如果你选择正确的编译器选项,编译器生成容易
调试的代码。)

There are several ways to designate that a function is inline, some of which involve the inline keyword, others do not. No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: it might inline-expand some, all, or none of the calls to an inline function. (Don't get discouraged if that seems hopelessly vague. The flexibility of the above is actually a huge advantage: it lets the compiler treat large functions differently from small ones, plus it lets the compiler generate code that is easy to debug if you select the right compiler options.)

在最简单的情况下,内联函数将被丢弃到其调用站点,就像您在那里进行了复制和粘贴一样。因此,

In the simplest case, the inline function is dropped into its call site as if you had copy-and-pasted it there. Thus for,

inline int madd( int a, int b, int c ) 
{
   return a * b + c;
}


void foo( int data[3] )
{
   int result = madd( data[0], data[1], data[2] );
   printf("%d\n", result); // note to pedants: this is simpler than a cout stream, so there
}

编译器可以将其转换为

void foo( int data[3] )
{
   int result = data[0] * data[1] + data[2] ; // madd is replaced inline
   printf("%d\n", result); 
}

这篇关于如何在调用位置替换Inline函数代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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