GCC 内联 C++ 函数是否没有“inline"关键字?

2023-05-09C/C++开发问题
8

本文介绍了GCC 内联 C++ 函数是否没有“inline"关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在编译 C++ 代码时,GCC 是否曾尝试通过选择内联未用 inline 关键字标记的函数来优化速度?

Does GCC, when compiling C++ code, ever try to optimize for speed by choosing to inline functions that are not marked with the inline keyword?

推荐答案

是的.任何编译器只要认为它是一个好主意,都可以自由地内联任何函数.GCC 也这样做.

Yes. Any compiler is free to inline any function whenever it thinks it is a good idea. GCC does that as well.

-O2 优化级别,内联在编译器认为值得做(使用启发式)并且如果它不会增加代码.在 -O3 处,只要编译器认为值得做,就会执行,而不管它是否会增加代码的大小.此外,在所有优化级别(即启用的优化)中,内联只调用一次的静态函数.

At -O2 optimization level the inlining is done when the compiler thinks it is worth doing (a heuristic is used) and if it will not increase the size of the code. At -O3 it is done whenever the compiler thinks it is worth doing, regardless of whether it will increase the size of the code. Additionally, at all levels of optimization (enabled optimization that is), static functions that are called only once are inlined.

正如下面的评论中所指出的,这些 -Ox 实际上是包含多个更具体设置的复合设置,包括与内联相关的设置(如 -finline-functions 和这样),所以我们也可以根据那些更具体的设置来描述行为(并控制它).

As noted in the comments below, these -Ox are actually compound settings that envelop multiple more specific settings, including inlining-related ones (like -finline-functions and such), so one can also describe the behavior (and control it) in terms of those more specific settings.

这篇关于GCC 内联 C++ 函数是否没有“inline"关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6