这是什么意思__inline__? [英] What does __inline__ mean?

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

问题描述

我想通过一些code我碰到这样一行来学C.阅读:

I am trying to learn C. Reading through some code I came across a line like this:

__inline__ void () ...

什么是 __ __内联是什么意思?而如何把这个词在函数前使其有什么不同?

What does the __inline__ mean? And how does putting that word in front of a function make it different?

推荐答案

__ __内联非标的扩展。通常情况下,它告诉编译器:内联这个函数,但作为一个非标准扩展,我们不能肯定地说,除非我们知道这是哪个编译器

__inline__ is a non-standard extension. Typically, it tells the compiler: "inline this function", but being a non-standard extension we can't say with certainty unless we know which compiler this is on.

要直列是去除函数呼叫,并把它直接的内容,其中该呼叫将被进行。这经常移除调用函数的开销。它并不总是最优的,因为code膨胀(code变得太大而不能装配到缓存),所以大多数编译器会忽略所有内嵌指令和做什么,他们的感觉是最好的。的这是一件好事。的我们人类是在那样的东西很差,它通常被认为是不好的做法,告诉编译器如何完成其​​工作。

To inline is to remove the function call and place it's contents directly where the call would be made. This often removes the overhead of calling a function. It is not always optimal, because of code bloat (code getting too big and not fitting into cache), so most compilers will ignore all inline directives and do what they feel is best. This is a good thing. We humans are very poor at that kind of stuff, and it's usually considered bad practice to tell the compiler how to do its job.

内联是一种重要的优化,特别是与辅助函数的presence。试想一下,返回的小两个整数的函数:

Inlining is an important optimization, especially with the presence of helper functions. Imagine a function that returned the smaller of two ints:

int min(int x, int y)
{
    return (x < y) ? x : y;
}

如果我在code使用此功能,这将是时候创建一个函数调用,这里的巨大浪费。如果我有:

If I used this function in my code, it would be an enormous waste of time to actually make a function call, here. If I had:

int a = /* some calculation */;
int b = /* some other calculation */;

int minCalc = min(a, b);

和编译器内联的功能,code将变成:

And the compiler inlined that function, the code would become:

int a = /* some calculation */;
int b = /* some other calculation */;

int minCalc = (a < b) ? a : b;

其中删除调用一个函数的开销。从这里开始,甚至更多的优化可以由作为编译器获取直接与code,将有通常被隐藏函数调用后面工作。正如你所看到的,如果我有一个很大的功能,我强制编译器到处内联吧,code尺寸可能增长的非常的大非常快,而且实际上会妨碍执行速度。

Which removes the overhead of calling a function. From here, even more optimizations can be made as the compiler gets to work directly with the code that would have normally been hidden behind a function call. As you can see, if I have a big function and I force the compiler to inline it everywhere, the code size could grow very large very fast, and would actually hinder execution speed.

有一个标准的在线关键字这是用来指示编译器函数应该内联,但现在大多数编译器甚至不承认它作为一个提示内联函数。

There is a standard inline keyword which was used to indicate to the compiler a function should be inlined, but nowadays most compilers don't even acknowledge it as a hint to inline the function.

内嵌的一个重要副作用,不过,这可能是有用的。如果函数被标记为在线,在多个翻译单元同样功能的多个定义是不是一个错误。相反,单一功能的定义被选中,别人都扔出去,并且假设是相同的(这是由你来确保这是的实际的好呀!)。这允许你定义一个头文件中的函数,而不用担心违反ODR的错误。

There is an important side-effect of inline, though, and this can be useful. If a function is marked as inline, multiple definitions of the same function across multiple translation units is not an error. Instead, a single function definition is selected and the others are thrown out, and assumed to be the same (it's up to you to make sure this is actually okay!). This allows you to define a function within a header file without risking ODR violation errors.

这篇关于这是什么意思__inline__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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