c++ - 什么时候使用内联函数,什么时候不使用它?

标签 c++ c inline

我知道内联是对编译器的提示或请求,它用于避免函数调用开销。

那么在什么基础上可以确定一个函数是否是内联的候选者呢? 在哪种情况下应该避免内联?

最佳答案

避免函数调用的成本只是故事的一半。

做:

  • 使用 inline 代替 #define
  • 非常小的函数非常适合inline:更快的代码和更小的可执行文件(更多的机会留在代码缓存中)
  • 函数很小并且经常被调用

不要:

  • 大型函数:导致更大的可执行文件,无论调用开销导致执行速度如何,都会显着降低性能
  • 受 I/O 限制的内联函数
  • 该功能很少使用
  • 构造函数和析构函数:即使为空,编译器也会为它们生成代码
  • 在开发库时破坏二进制兼容性:
    • 内联现有函数
    • 更改内联函数或使内联函数非内联:库的早期版本调用旧实现

在开发库时,为了使类在未来可扩展,您应该:

  • 即使主体为空,也可以添加非内联虚拟析构函数
  • 使所有构造函数非内联
  • 编写复制构造函数和赋值运算符的非内联实现,除非类不能按值复制

请记住,inline 关键字是对编译器的提示:编译器可能决定不内联函数,它可以决定内联未标记为 inline 的函数首先。我通常避免标记函数 inline (可能在编写非常非常小的函数时除外)。

关于性能,明智的做法是(一如既往)分析应用程序,然后最终内联一组代表瓶颈的函数。

引用资料:


编辑:Bjarne Stroustrup,C++ 编程语言:

A function can be defined to be inline. For example:

inline int fac(int n)
{
  return (n < 2) ? 1 : n * fac(n-1);
}

The inline specifier is a hint to the compiler that it should attempt to generate code for a call of fac() inline rather than laying down the code for the function once and then calling through the usual function call mechanism. A clever compiler can generate the constant 720 for a call fac(6). The possibility of mutually recursive inline functions, inline functions that recurse or not depending on input, etc., makes it impossible to guarantee that every call of an inline function is actually inlined. The degree of cleverness of a compiler cannot be legislated, so one compiler might generate 720, another 6 * fac(5), and yet another an un-inlined call fac(6).

To make inlining possible in the absence of unusually clever compilation and linking facilities, the definition–and not just the declaration–of an inline function must be in scope (§9.2). An inline especifier does not affect the semantics of a function. In particular, an inline function still has a unique address and so has static variables (§7.1.2) of an inline function.

EDIT2:ISO-IEC 14882-1998,7.1.2 功能说明符

A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected.

关于c++ - 什么时候使用内联函数,什么时候不使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1932311/

相关文章:

c++ - 使用/Wp64 编译时如何在没有警告的情况下将 strlen 的结果转换为 int

python - Ctypes:OSError:异常:堆栈溢出

c++ - C++ 中的跨操作系统分布式计算,rand() 问题

c - 得到警告

c - C 语言 gcc 编译器中前后增量的意外行为

compiler-construction - F# 编译器如何/何时提前读取类型推断?

html - 顶部菜单 CSS - 有对齐和链接问题

c++ - 如何在 Boost.MPL 中使用嵌套元函数?

c - 哪个更快 - 排序或乘以一个小的元素数组?

javascript - javascript onchange 中的代码与函数中的相同代码有什么区别?