C“内联”的替代方案关键词
我的课程导师一再强调并要求我们不要在函数中使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的课程老师错了。这是标准的。它实际上位于当前标准中,位于
6.7.4 函数说明符
(C99) 部分。事实上,这是对编译器的建议,可能会被完全忽略,但这并不会降低它的标准。我不认为它是在 C89/90 中,这可能是某些嵌入式编译器使用的,但在这种情况下我会认真考虑升级。
然而,即使可以使用内联,我通常也会将这些决定留给编译器本身,因为大多数现代编译器都能够弄清楚如何最好地优化代码(并且通常比我好得多)。
inline
关键字,如register
和auto
,根本不是我通常担心的事情。您可以使用宏,因为这是相对简单的文本替换,通常发生在编译阶段之前,但您应该意识到其限制和缺点。
或者您可以手动内联代码(即复制它),尽管我不建议将此作为一个选项,因为它可能很快就会成为维护噩梦。
我自己,我会使用普通函数编写代码,而不使用任何这些技巧,然后在必要时引入它们(并且仅如果您可以证明需要它们,例如特定的性能问题)。
你应该始终假设维护你的代码的程序员是一个心理变态的杀手,他知道你住在哪里:-)
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). Theinline
keyword, likeregister
andauto
, 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 :-)
正如其他人所说,
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 onlystatic
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
尽管它们可能很邪恶,但宏仍然是王道(尽管特定的编译器可能支持额外的功能)。
As evil as they may be, macros are still king (although specific compilers may support extra capabilities).
在这里,现在它是“跨编译器可移植的”:
顺便说一句,正如其他人所说,
inline
关键字只是一个提示,毫无用处,但重要的关键字是static
。除非您的函数被声明为静态,否则它将具有外部链接,并且编译器在自行决定内联哪些函数时不太可能将其视为内联的候选者。另请注意,与 C++ 不同,C 语言不允许没有static
的内联
。Here, now it's "portable across compilers":
By the way, as others have said, the
inline
keyword is just a hint and rather useless, but the important keyword isstatic
. Unless your function is declaredstatic
, 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 allowinline
withoutstatic
.