内联定义和声明
在最近的一次同行评审中,另一位软件工程师建议我声明 inline
函数在定义(类外部)和声明(类内部)中都是 inline
。班级)。他的论点是“通过将其标记为内联,您可以说该方法的执行速度比非内联方法快得多,并且调用者不必担心对该方法的过度调用。“
这是真的吗?如果我是一个类的用户,我真的关心对该方法的过度调用吗?在定义和声明中将其列为内联有什么问题吗? C++ 常见问题解答指出:
最佳实践:仅在类主体之外的定义中。
那么谁在这里呢?
During a recent peer review, another Software Engineer suggested that I state that the inline
function is inline
in the definition (outside of the class) as well as in the declaration (inside of the class). His argument is that "By marking it inline, you are saying that this method will be executed much faster than a non-inline method, and that callers don't have to worry about excessive calls to the method."
Is that true? If I am a user of a class, do I really care about excessive calls to the method? Is there anything wrong with listing it as inline in both the definition and declaration? The C++ FAQ states:
Best practice: only in the definition outside the class body.
So who is right here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那是无稽之谈。将函数标记为内联并不能保证该函数实际上会被物理内联;即使是这样,也不能保证你的函数会“更快”。
通过将定义标记为内联以及声明,您只是通过向用户假装对任何事情有任何保证而混淆了事情,而实际上并没有......
并不真地。
事实上,真正应该编写
inline
的唯一时间是当您出于某种原因需要强制内联存储时(无论是否发生内联,使用关键字总是会影响将单一定义规则应用于您的函数……尽管很少需要这样做);否则,让编译器决定内联哪些函数,然后继续。其必然结果是,您无需担心使用关键字来假装它正在记录任何内容。That's nonsense. Marking a function
inline
doesn't guarantee that the function will actually be physically inlined; even if it is, that's no guarantee that your function will be "faster".By marking the definition
inline
as well as the declaration, you're just confusing things by pretending to your user that there's any guarantee about anything, which there isn't...Not really.
In fact, really, the only time you should write
inline
is when you need to force inline storage for some reason (regardless of whether inlining occurs, using the keyword always affects the application of the one-definition rule to your function… though requiring this is rare); otherwise, let the compiler decide which functions to inline, and move on. The corollary of this is that you don't need to worry about using the keyword to pretend that it's documenting anything.头文件中定义的函数定义应使用内联说明符。
foo.h 中定义的示例 double get_f(){return f;}
应该使用内联说明符,如下所示:
内联双 f_get{return f};
根据 C++ 指南。
至于标题外部的内联,我还没有看到任何表明需要它的信息。
A function definition defined in the header file should use the inline specifier.
example double get_f(){return f;} defined in foo.h
should use the inline specifier as in:
inline double f_get{return f};
According to the C++ guidelineS.
As for inlining outside the header I have not seen any information that would suggest it is needed.
这听起来像是两件完全不相关的事情。不需要将
inline
放在类中的声明和类外部的定义中。将其放在其中一个声明中就足够了。如果您谈论在定义了函数的
.cpp
文件中添加inline
,并且该函数是public
成员,那么您应该不这样做。调用内联函数的人必须使其定义对他们可见。 C++ 标准要求这样做。That sounds like two totally unrelated things. Putting
inline
at both the declaration in the class and the definition outside of the class is not needed. Putting it at one of the declarations is enough.If you talk about adding
inline
in a.cpp
file where a function is defined there, and that function is apublic
member, then you should not do that. People who callinline
functions must have their definitions visible to them. The C++ Standard requires that.