什么是使用在C`inline`关键字?

什么是使用在C`inline`关键字?

问题描述:

我读计算器约在线用C的几个问题,但仍然是不清楚的。

I read several questions in * about inline in C but still am not clear about it.


  1. 静态内嵌无效F(无效){} 静态无效F(无效)没有实际差别{}

  2. 内嵌无效F(无效){} 在C不作为C ++的方式工作。如何在C工作?

  3. 什么实际执行 extern内联无效F(无效);

  1. static inline void f(void) {} has no practical difference with static void f(void) {}.
  2. inline void f(void) {} in C doesn't work as the C++ way. How does it work in C?
  3. What actually does extern inline void f(void); do?

我从来没有真正找到了一个使用在线关键字在我的C程序,当我看到其他人的code这个关键字,它几乎总是静态内联,我在其中看到刚刚静态无差异

I never really found a use of the inline keyword in my C programs, and when I see this keyword in other people's code, it's almost always static inline, in which I see no difference with just static.

请注意:当我谈到 .C 文件和 .H 在这个应答文件,我假设你已经制定了您的code正确,即 .C 文件只包含 .H 文件。区别是, .H 文件可能包含在多个翻译单元。

Note: when I talk about .c files and .h files in this answer, I assume you have laid out your code correctly, i.e. .c files only include .h files. The distinction is that a .h file may be included in multiple translation units.

静态内嵌无效F(无效){} 静态无效F(无效){}没有实际的区别

在ISO C,这是正确的。他们在行为上是相同的(假设你没有在不同当然同样TU重新声明它们!)唯一的实际效果可能会导致编译器优化不同。

In ISO C, this is correct. They are identical in behaviour (assuming you don't re-declare them differently in the same TU of course!) the only practical effect may be to cause the compiler to optimize differently.

内嵌无效F(无效){} 在C不作为C ++的方式工作。如何在C工作?什么实际执行 extern内联无效F(无效);

inline void f(void) {} in C doesn't work as the C++ way. How does it work in C? What actually does extern inline void f(void); do?

这是由这个答案也是this螺纹。

在ISO C和C ++,您可以*使用内嵌无效F(无效){} 头文件 - 尽管出于不同的原因。

In ISO C and C++, you can freely use inline void f(void) {} in header files -- although for different reasons!

在ISO C中,它不提供一个外部定义的。在ISO C ++它确实提供了一个外部定义;然而C ++有一个额外的规则(其中C没有),如果有一个在线函数的多个外部定义,那么编译器进行排序并挑选其中之一。

In ISO C, it does not provide an external definition at all. In ISO C++ it does provide an external definition; however C++ has an additional rule (which C doesn't), that if there are multiple external definitions of an inline function, then the compiler sorts it out and picks one of them.

extern内联无效F(无效); 在ISO C一 .C 文件,就是要配对与使用头文件内嵌无效F(无效){} 的。它导致的外部定义的函数的在该翻译单元发射。如果你不这样做,那么就没有外部定义,所以你可能会得到一个链接错误(这是不确定是否的任何特定调用F 链接到外部定义或不)。

extern inline void f(void); in a .c file in ISO C is meant to be paired with the use of inline void f(void) {} in header files. It causes the external definition of the function to be emitted in that translation unit. If you don't do this then there is no external definition, and so you may get a link error (it is unspecified whether any particular call of f links to the external definition or not).

在换句话说,在ISO C您可以手动选择的外部定义去;或SUP preSS外部定义完全使用静态内联无处不在;但在ISO C ++编译器,如果选择,且外部定义woudd去了。

In other words, in ISO C you can manually select where the external definition goes; or suppress external definition entirely by using static inline everywhere; but in ISO C++ the compiler chooses if and where an external definition woudd go.

在GNU C,情况就不同了(详见下文)。

In GNU C, things are different (more on this below).

要的事情进一步复杂化,GNU C ++允许你写静态内联 extern内联在C ++中$ C $ ç...我不想去猜测什么,做究竟

To complicate things further, GNU C++ allows you to write static inline an extern inline in C++ code... I wouldn't like to guess on what that does exactly

我从来没有真正找到了使用inline关键字在我的C程序,当我看到其他人的code这个关键字,它几乎总是静态的内联

I never really found a use of the inline keyword in my C programs, and when I see this keyword in other people's code, it's almost always static inline

许多codeRS不知道他们在做什么,只是放在一起,似乎工作的一些东西。这里的另一个因素是,你正在看的GNU C,不ISO C.

Many coders don't know what they're doing and just put together something that appears to work. Another factor here is that the code you're looking at might have been written for GNU C, not ISO C.

GNU C ,平原在线不同的行为与ISO C.它实际上发出一个外部可见的定义,所以有两个包含一个 .H 用纯文本文件在线功能翻译单元会导致不确定的行为。

In GNU C, plain inline behaves differently to ISO C. It actually emits an externally visible definition, so having a .h file with a plain inline function included from two translation units causes undefined behaviour.

因此​​,如果codeR想在GNU C供应在线优化提示,那么静态内联是必须的。由于静态内联在这两个ISO C和GNU C的作品,这是自然的,人们最终解决了这一点,看到这似乎不给错误的工作。

So if the coder wants to supply the inline optimization hint in GNU C, then static inline is required. Since static inline works in both ISO C and GNU C, it's natural that people ended up settling for that and seeing that it appeared to work without giving errors.

,其中我看到只是静态的没有区别。

, in which I see no difference with just static.

不同的是刚刚在意图提供一种速度过度尺寸优化暗示给编译器。随着现代编译器,这是多余的。

The difference is just in the intent to provide a speed-over-size optimization hint to the compiler. With modern compilers this is superfluous.