简体   繁体   English

在“类定义”中定义的C ++成员函数中隐式“内联”

[英]Is “inline” implicit in C++ member functions defined in class definition

According to the C++ specification, are the following two classes equivalently defined? 根据C ++规范,以下两个类是否等效定义?

class A
{
   void f()
   {
   }
};

class B
{
   inline void f()
   {
   }
};

ie, 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 :) 谢谢 :)

The C++ ISO standard says: C ++ ISO标准说:

"A function defined within a class definition is an inline function." “在类定义中定义的函数是内联函数。”

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. 但是,这并不意味着函数必然会被内联:通常现在,编译器似乎会决定内联函数是否会带来任何好处。

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. 因此,该标准不保证您可以使用一个类定义和另一个TU编译一个TU(转换单元),然后将它们链接在一起。 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. inline关键字与内联几乎没有任何关系。 It's about whether multiple identical definitions of the function are permitted in different TUs. 它是关于在不同的TU中是否允许多个相同的函数定义。 If someone moves the function definition elsewhere, then they should decide whether to mark it inline on the following basis: 如果有人将函数定义移动到其他位置,那么他们应该根据以下基础决定是否将其标记为inline

  • If it is in a .cpp file for that class, then it's valid to mark it inline if it's called only from that TU. 如果它在该类的.cpp文件中,那么只有从该TU调用它才能将其标记为inline Then it probably makes no difference whether it is marked inline or not, but you could mark it inline as a compiler hint if you think the compiler will pay any attention to what you want. 然后它是否标记为inline可能没有区别,但如果您认为编译器会关注您想要的内容,则可以将其标记为inline编译器提示。

  • 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. 如果它仍然在头文件中,则必须将其标记为inline ,否则在链接使用标头的不同TU时会出现多个定义错误。

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关键字来移动它会更安全。

is putting the "inline" qualifier on such member function defined in the class definition completely redundant? 将“内联”限定符放在类定义中定义的成员函数上是完全冗余的吗?

Yes

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? 对于代码样式,保留“内联”标记是否合理,以便未来的开发人员意识到应该内联函数,并且不会删除其他地方的定义并删除内联?

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. 如果定义了需要inline的函数并且未提供它,则它是编译时错误。 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. 最好给出一个详细的评论来解释为什么函数必须内联(其中我只能解释两个原因:“它是一个模板”,在这种情况下,衬里是不可能的,或者“性能”在这种情况下我想看到一些支持证据,而不是“它必须表现得更好,因为它是内联的”,我在某些地方看到过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM