是“内联”的吗? C++ 中隐含的类定义中定义的成员函数
根据C++规范,以下两个类的定义是否等效?
class A
{
void f()
{
}
};
class B
{
inline void f()
{
}
};
即,将“内联”限定符放在类定义中定义的此类成员函数上是否完全多余?
后续问题:假设它是多余的,对于代码风格,保留“内联”标签是否明智,以便未来的开发人员意识到函数应该内联,并且不会删除其他地方的定义并删除内联?
谢谢 :)
According to the C++ specification, are the following two classes equivalently defined?
class A
{
void f()
{
}
};
class B
{
inline void f()
{
}
};
i.e., is putting the "inline" qualifier on such member function defined in the class definition completely redundant?
Followon question: Assuming it is redundant, for code style, would it be sensible to keep the "inline" tag, so a future developer realises that function should be inlined, and does not remove the definition somewhere else and remove the inlining?
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ ISO 标准规定:
但是,这并不意味着该函数一定会被内联:现在通常情况下,编译器会决定内联该函数是否会带来任何好处。
The C++ ISO standard says:
But, this doesn't mean the function will necessarily be inlined: generally nowadays, it appears that the compiler will decide if inlining the function will lead to any benefits.
除了单一定义规则之外,它们是等效的类定义。因此,该标准不保证您可以使用一个类定义编译一个 TU(翻译单元),并使用另一个类定义编译不同的 TU,然后将它们链接在一起。我怀疑这在真正的实施中实际上会失败,但这就是标准所说的。
inline
关键字与内联几乎没有任何关系。这是关于在不同的 TU 中是否允许该函数有多个相同的定义。如果有人将函数定义移到其他地方,那么他们应该根据以下基础决定是否将其标记为内联:如果它位于该类的
.cpp
文件中,那么如果仅从该 TU 调用它,则将其标记为内联是有效的。那么它是否被标记为内联可能没有什么区别,但是如果您认为编译器会关注您的内容,您可以将其标记为内联作为编译器提示inline
,否则在链接使用该标头的不同 TU 时,您将收到多个定义错误。假设移动函数的人知道这些事情,我认为他们不需要在类定义中提醒。如果他们不知道这些事情,那么他们可能没有必要移动该函数,但使用
inline
关键字来移动该函数对他们来说会更安全。They're equivalent class definitions except for the purposes of the One Definition Rule. So the standard does not guarantee that you can compile one TU (translation unit) with one class definition and a different TU with the other, and then link them together. I doubt that this would ever actually fail on a real implementation, but that's what the standard says.
The
inline
keyword has approximately nothing to do with inlining. It's about whether multiple identical definitions of the function are permitted in different TUs. If someone moves the function definition elsewhere, then they should decide whether to mark itinline
on the following basis:If it is in a
.cpp
file for that class, then it's valid to mark itinline
if it's called only from that TU. Then it probably makes no difference whether it is markedinline
or not, but you could mark itinline
as a compiler hint if you think the compiler will pay any attention to what you want.If it is still in the header file, then it must be marked
inline
, or else you'll get multiple definition errors when linking different TUs that use the header.Assuming that the person moving the function knows those things, I don't think they need a reminder in the class definition. If they don't know those things, then they probably have no business moving the function, but it would be safer for them to have an
inline
keyword to move with it.是的
编号
内联用于“一个定义规则”(因此通过扩展进行链接)。如果函数在需要
inline
的地方定义,但未提供,则会出现编译时错误。如果不需要,那么它只是多余的无用的绒毛。因此,如果您不需要它,请将其删除。如果您需要它,请将其放在那里(如果您忘记了,编译器会通知您)。
Yes
No.
The inline is for "One Definition Rule" (and therefore linking by extension). If the function is defined where
inline
is required and it is not provided it is a compile time error. If it is not needed then it is just extra useless fluff.So if you don't need it remove it. If you need it put it there (if you forget the compiler will let you know).
在这种情况下,内联是可选的,是的。只要包含标头,编译器就会知道它是内联的,并且不会生成该函数的多个实例。
至于把它留在那里是否是个好主意——我真的不这么认为。最好给出详细的注释来解释为什么该函数必须内联(其中我只能想到两个原因:要么“它是一个模板”,在这种情况下,超出内联是不可能的,要么是“性能”,在这种情况下我希望看到一些支持证据,而不是我在某些地方看到的“它必须表现得更好,因为它是内联的”。
The inline is optional in that case, yes. The compiler will know it's inline and not generate multiple instances of the function whenever the header is included.
As to whether its a good idea to leave it there - I don't really think so. It'd be better to give a detailed comment explaining why the function MUST be inlined (of which I can thin of only 2 reasons: either "it's a template" in which case out of lining is impossible, or "performance" in which case I'd want to see some supporting evidence, rather than the "it has to perform better because it's inline" that I've seen in some places.