简体   繁体   English

函数定义的顺序对于C ++中的内联是否很重要

[英]Is the order of function definitions important for inlining in C++

When one function calls another, and inlining is desired, is the order of the definitions of the two functions important? 当一个函数调用另一个函数并且需要内联时,这两个函数的定义顺序是否重要? Assume that the two definitions occur in the same translation unit. 假设这两个定义出现在同一个翻译单元中。

I am primarily interested in what the C++ standard says about it, if anything. 我主要对C ++标准所说的内容感兴趣,如果有的话。 However, if you have important information about the inlining behavior in specific compilers, I would be interested in that too. 但是,如果您有关于特定编译器中的内联行为的重要信息,我也会对此感兴趣。 Please assume that no link-time optimization occurs (is disabled). 请假设没有发生链接时优化(被禁用)。

Specifically, are the following two versions equally likely to achieve inlining according to the C++ standard? 具体来说,根据C ++标准,以下两个版本是否同样可以实现内联?

Version one: 第一版:

inline void foo() { ... }
void bar() { ... foo(); ... }

Version two: 第二版:

inline void foo();
void bar() { ... foo(); ... }
void foo() { ... }

EDIT: Please note that this question is not about the effectiveness of the inline keyword with respect to achieving inlining in general. 编辑:请注意,这个问题不是关于inline关键字在实现内联方面的有效性。 I specifically ask about whether the standard says anything about the order of function definitions in relation to achieving inlining. 我特别询问标准是否说明了与实现内联相关的函数定义的顺序

The C++ standard doesn't make any imposition here. C ++标准不会在此处进行任何拼版。 Compilers are free to inline or not in any situation they see fit. 编译器可以在任何他们认为合适的情况下自由内联或不内联。 The inline keyword may have no effect other than allowing multiple definitions of the same function. 除了允许同一函数的多个定义之外,inline关键字可能没有任何效果。

Most compilers will be able to inline any function that is available to the translation unit, regardless of the order it appears in the source. 大多数编译器都能够内联翻译单元可用的任何功能,无论它在源中出现的顺序如何。 Forward declarations generally only affect what names are available at a given point in the source file, not anything about the final output of the binary. 前向声明通常只会影响源文件中给定点的可用名称,而不会影响二进制文件的最终输出。

In reality the inline keyword has less to do with inlining code and more to do with allowing legal violation of the one definition rule. 实际上, inline关键字与内联代码的关系较少,而与允许legal violation一个定义规则有关。 The main purpose of inline is to tell the compile that a function may appear in multiple translation units (and that it will have the same definition in each one). 内联的主要目的是告诉编译一个函数可能出现在多个翻译单元中(并且每个翻译单元中的定义都相同)。 This allows it to avoid multiple definition errors at the link stage. 这允许它避免链接阶段的多个定义错误。

Using the inline keyword is no guarantee that the compiler will inline the function at all - it's just a suggestion. 使用inline关键字并不能保证编译器完全内联函数 - 这只是一个建议。 Additionally many compilers will inline functions not marked as inline if the definition can be seen within the translation unit and the compiler deems it worthwhile. 此外,如果可以在翻译单元中看到定义并且编译器认为值得,则许多编译器将内联函数未标记为内联。 I strongly suspect that either version you show would either be inlined or not by a particular compiler. 我强烈怀疑你展示的任何一个版本都会被特定的编译器内联或不内联。

In short, let the compiler decide whether or not to inline, and write your code in the most readable way. 简而言之,让编译器决定是否内联,并以最易读的方式编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM