C ++内联函数和重新声明 [英] C++ inline functions and redeclarations

本文介绍了C ++内联函数和重新声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!



First of all, sorry for my English.


Since GCC totally ignores inline specifiers, it's a little difficult for me to know when a funcion has been marked inline by me or not. What I'm trying to understand is, when you have some redeclarations of a same function (in a same translation unit or in different ones), when or under which circunstances your function is marked inline or not (irrespective of what will the compiler make with your hint).


inline void print();

void print();


void print();
inline print();


What is the meaning of redeclaring a function with different inline specifiers? A more complex example:

#include <iostream>

void print();

int main()
    print(); // (1)

inline void print() { std::cout << "HELLO" << std::endl; }

从C ++的排他性角度来看,而不是从编译器的角度来看,是 print 函数在行(1)?内联一个吗?

From the exclusive point of view of C++ and not from the point of view of a compiler, is the print function an inline one at line (1)?

我不能简洁地提出问题,但是我认为已经收到消息了:)我试图从C ++和程序员的角度(无论如何)来理解何时应该将函数标记为内联,何时不应该标记为内联

I cannot formulate my question conciselly, but I think the message has been received :) I'm trying to understand when a function is supposed to be marked inline and when not from the point of view of C++ and the programmer's (irrespective of what will the compiler make with your function).



inline allows an external linkage function to be defined (essentially identically) in two or more translation units.


which means defining it in a header.

当函数为 inline 时,必须对其进行定义,而 inline 必须为 1) 同样,在使用它的每个翻译单元中。

when a function is inline it must be defined, and inline, and essentially 1)identically, in every translation unit where it's used.

inline 只是累加特斯函数不能取消-内联

inline just accumulates. a function can't become un-inline.


in addition to these considerations inline serves as an optimization hint, that the compiler may freely ignore. in that capacity it states that you would like the machine code calls to this function to be inlined. generally the compiler can do a better job of deciding where that's a good idea or not.

1 C ++ 17 10.1.6 / 5(dcl.inline / 5):内联函数或变量应在使用过的每个翻译单元中定义,并在其中具有完全相同的定义每种情况

1 C++17 10.1.6/5 (dcl.inline/5): "An inline function or variable shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case"

这篇关于C ++内联函数和重新声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
发送“验证码”获取 | 15天全站免登陆