C“内联”的替代方案关键词

发布于 2024-10-04 12:05:26 字数 94 浏览 1 评论 0原文

我的课程导师一再强调并要求我们不要在函数中使用“inline”关键字。他说它不能跨编译器“移植”,也不是“标准”。考虑到这一点,是否有任何允许“内联扩展”的“标准”替代方案?

From my course instructor, he has repeatedly emphasized and asked us not to use the "inline" keyword for functions. He says it is not "portable" across compilers and is not "standard". Considering this, are there any "standard" alternatives that allow for "inline expansion"?

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

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

发布评论

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

评论(4

狼性发作 2024-10-11 12:05:26

你的课程老师错了。这是标准的。它实际上位于当前标准中,位于 6.7.4 函数说明符 (C99) 部分。事实上,这是对编译器的建议,可能会被完全忽略,但这并不会降低它的标准。

我不认为它是在 C89/90 中,这可能是某些嵌入式编译器使用的,但在这种情况下我会认真考虑升级。

然而,即使可以使用内联,我通常也会将这些决定留给编译器本身,因为大多数现代编译器都能够弄清楚如何最好地优化代码(并且通常比我好得多)。 inline 关键字,如 registerauto,根本不是我通常担心的事情。

您可以使用宏,因为这是相对简单的文本替换,通常发生在编译阶段之前,但您应该意识到其限制和缺点。

或者您可以手动内联代码(即复制它),尽管我不建议将此作为一个选项,因为它可能很快就会成为维护噩梦。

我自己,我会使用普通函数编写代码,而不使用任何这些技巧,然后在必要时引入它们(并且如果您可以证明需要它们,例如特定的性能问题)。

你应该始终假设维护你的代码的程序员是一个心理变态的杀手,他知道你住在哪里:-)

Your course instructor is wrong. It is standard. It's actually in the current standard, right there in section 6.7.4 Function specifiers (C99). The fact that it's a suggestion to the compiler that may be totally ignored does not make it any less standard.

I don't think it was in C89/90 which may be what some embedded compilers use but I would give serious consideration to upgrading in that case.

However, even where inline is available, I generally leave those decisions up to the compiler itself since most modern ones are more than capable of figuring out how best to optimise code (and usually far better than I). The inline keyword, like register and auto, is not something I normally worry about at all.

You can use macros instead since that's relatively simple text substitution that generally happens before the compile phase but you should be aware of the limitations and foibles.

Or you can manually inline code (ie, duplicate it) although I wouldn't suggest this as an option since it may quickly become a maintenance nightmare.

Myself, I would write the code using normal functions without any of those tricks and then introduce them where necessary (and only if you can demonstrate that they're needed, such as a specific performance issue).

You should always assume that the coder who has to maintain your code is a psychopathic killer who knows where you live :-)

三生殊途 2024-10-11 12:05:26

正如其他人所说,inline 已于 11 年前集成到 C 标准中。

除了所指出的之外,内联会产生影响,因为它改变了函数的可见性属性。特别是对于具有许多仅声明为static的函数的大型库,您可能在所有对象文件中拥有这些函数的一个版本(例如,当您在打开调试的情况下进行编译时) 。

请查看该帖子:关于 C99 中内联的神话与现实

As others have said, inline was integrated to the C standard 11 years ago.

Other than was indicated, inline makes a difference since it changes the visibility properties of the function. In particular for large libraries with a lot of functions declared only static you might have one version of any these function in all object files (e.g when you compile with debugging switched on).

Please have a look into that post: Myth and reality about inline in C99

旧时光的容颜 2024-10-11 12:05:26

尽管它们可能很邪恶,但宏仍然是王道(尽管特定的编译器可能支持额外的功能)。

As evil as they may be, macros are still king (although specific compilers may support extra capabilities).

友谊不毕业 2024-10-11 12:05:26

在这里,现在它是“跨编译器可移植的”:

#if (__STDC_VERSION__ < 199901L)
#define inline
#endif
static inline int foobar(int x) /* ... */

顺便说一句,正如其他人所说,inline关键字只是一个提示,毫无用处,但重要的关键字是static 。除非您的函数被声明为静态,否则它将具有外部链接,并且编译器在自行决定内联哪些函数时不太可能将其视为内联的候选者。

另请注意,与 C++ 不同,C 语言不允许没有 static内联

Here, now it's "portable across compilers":

#if (__STDC_VERSION__ < 199901L)
#define inline
#endif
static inline int foobar(int x) /* ... */

By the way, as others have said, the inline keyword is just a hint and rather useless, but the important keyword is static. Unless your function is declared static, it will have external linkage, and the compiler is unlikely to consider it a candidate for inlining when it makes its own decisions about which functions to inline.

Also note that unlike in C++, the C language does not allow inline without static.

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