Can modern C++ compilers inline functions that are defined in a cpp file(现代 C++ 编译器可以内联 cpp 文件中定义的函数吗)
问题描述
我知道关键字 inline
具有有用的属性,例如用于将模板特化保存在头文件中.另一方面,我经常读到 inline
作为编译器实际内联函数的提示几乎毫无用处.此外,该关键字不能在 cpp 文件中使用,因为编译器希望在调用时检查标有 inline
关键字的函数.
I am aware that the keyword inline
has useful properties e.g. for keeping template specializations inside a header file.
On the other hand I have often read that inline
is almost useless as hint for the compiler to actually inline functions.
Further the keyword cannot be used inside a cpp file since the compiler wants to inspect functions marked with the inline
keyword whenever they are called.
因此,我对现代编译器(即 gcc 4.43)的自动"内联功能有点困惑.当我在 cpp 中定义一个函数时,如果编译器认为内联对函数有意义,或者我是否剥夺了他的一些优化能力,编译器是否可以内联它?(这对于大多数函数来说都很好,但对于经常调用的小函数来说很重要)
Hence I am a little confused about the "automatic" inlining capabilities of modern compilers (namely gcc 4.43). When I define a function inside a cpp, can the compiler inline it anyway if it deems that inlining makes sense for the function or do I rob him of some optimization capabilities ? (Which would be fine for the majority of functions, but important to know for small ones called very often)
推荐答案
这取决于你的编译标志.使用 -combine
和 -fwhole-program
,gcc
将跨 cpp 边界进行函数内联.如果你编译成多个目标文件,我不确定链接器会做多少.
This depends on your compilation flags. With -combine
and -fwhole-program
, gcc
will do function inlining across cpp boundaries. I'm not sure how much the linker will do if you compile into multiple object files.
这篇关于现代 C++ 编译器可以内联 cpp 文件中定义的函数吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:现代 C++ 编译器可以内联 cpp 文件中定义的函数吗


- C++ 协变模板 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 静态初始化顺序失败 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- STL 中有 dereference_iterator 吗? 2022-01-01