是否有编译器忽略 C++ 关于默认内联函数的标准?

发布于 2024-07-15 03:20:03 字数 198 浏览 6 评论 0原文

C++ ISO 标准规定:

"A function defined within a class definition is an inline function."

是否有编译器忽略此规则?
(请不要将内联误认为是 inlineD - 我的问题是是否有一个编译器,它不会将内联建议放在那里)

C++ ISO standard says, that:

"A function defined within a class definition is an inline function."

Are there any compilers that IGNORE this rule?
(please, do not mistake inline with inlineD - my question is if there is a compiler, that wont put there that inline suggestion that it should)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雨落星ぅ辰 2024-07-22 03:20:03

您似乎误解了“内联”的含义。 这并不意味着函数会自动内联;而是意味着函数会自动内联。 根据7.1.2-2,它表明内联替换是首选。

因此,您无法从代码中判断函数是否被标记为内联,因为编译器可以自由决定一种或另一种方式。 这只是一个编译器提示。

You seem to be misunderstanding what "inline" means. It doesn't mean functions will automatically be inlined; according to 7.1.2-2 it indicates that inline substitution is to be preferred.

Therefore, you can't tell whether a function is labeled inline or not from the code, since the compiler is free to decide one way or another. It's just a compiler hint.

梦里南柯 2024-07-22 03:20:03

该标准规定所有编译器都可以忽略内联请求,无论是隐式的还是显式的。 它们是否这样做通常取决于该函数实际上是否可以内联 - 例如递归函数不能。

编辑:只是为了澄清 - 提问者忽略了这一点,从标准中的前一段到他引用的内容:

不需要实施
执行此内联替换
调用点

The standard says that all compilers can ignore inline requests, whether implicit or explicit. Whether or not they do so will nornally depend on whether the function can practically be inlined - for example recursive functions cannot be.

Edit: Just to clarify - the questioner is ignoring this, from the previous para in the standard to that he quoted from:

An implementation is not required to
perform this inline substitution at
the point of call

等数载,海棠开 2024-07-22 03:20:03

我怀疑你的测试有缺陷。 您无法仅使用一个这样的文件来测试编译器是否忽略内联说明符。

您需要包含包含内联函数定义的标头,并将其包含到多个实现文件中,然后将这些实现文件链接在一起。 如果您收到有关该函数的多个定义实例的链接器错误,则编译器将忽略有关其最重要属性的内联说明符:允许在整个程序中多次定义它,同时仍保留相同的属性它及其局部静态变量的地址。

您的测试可能检查的是编译器是否内联对函数的调用,这实际上只是对编译器的提示,并且只是内联说明符的许多其他更重要结果中的一小部分。 如果编译器不内联对该函数的调用,那么这样做就可以了。 标准没有要求它在这件事上做任何事情。

I suspect your test is flawed. You can't test with only one such file whether the compiler ignores the inline specifier or not.

You need to include the header containing the inline function definition and include it into multiple implementation files that are then linked together. If you get linker errors about multiple defined instances of that functions, then the compiler is ignoring the inline specifier regarding its most important property: Allowing it to be defined multiple times across the entire program while still retaining the same address for it and its local static variables.

What your test probably checks is whether or not the compiler inlines the call to the function, which is actually only a hint to the compiler and only a small of many other more important consequences of the inline specifier. If the compiler does not not inline a call to the function, it is fine doing so. The standard does not require it to do anything in this matter.

中二柚 2024-07-22 03:20:03

请参阅我对一个非常类似问题的回答:什么时候“内联”无效? (在C中)

总结:inline只需要允许多个定义。 任何调用更改的函数都是纯粹可选的。

See my answer to a very similar question: When is "inline" ineffective? (in C)

Summary: inline is only required to allow multiple definitions. Any function calling changes is purely optional.

暮光沉寂 2024-07-22 03:20:03

编译器的内联通常基于函数的调用次数、函数中伪指令的数量以及其他一些因素。 查看 GCC 文档优化选项以了解其工作方式。 基本上,inline 关键字只是一个提示,可以提高编译器内联的可能性。 内联的实际决定通常很复杂。

Compiler's usually inline based on the number of calls to the function, the number of pseudo-instructions in the function, and a bunch of other things. Take a look at the GCC documentation on optimization options for an idea of how it does things. Basically, the inline keyword is just a hint that bumps up the likelihood that the compiler will inline. The actual decision to inline is usually complex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文