简体   繁体   English

为什么不将boost :: shared_ptr - >运算符声明为内联?

[英]Why isn't the boost::shared_ptr -> operator declared inline?

Since boost::shared_ptr could be called very frequently and simply returns a pointer, isn't the -> operator a good candidate for being inlined ? 由于boost::shared_ptr可以非常频繁地调用并且只是返回一个指针,因此->运算符不是inlined的良好候选者吗?

T * operator-> () const // never throws
{
    BOOST_ASSERT(px != 0);
    return px;
}

Would a good compiler automatically inline this anyway? 一个好的编译器会自动inline这个吗?

Should I lose any sleep over this? 我应该失眠吗? :-) :-)

Functions defined (ie with a body) inside a class are implicitly candidates for inlining. 在类中定义的函数(即使用正文)是内联的隐式候选者。 There is no need to use the inline keyword in these cases, and it is unusual to do so. 在这些情况下不需要使用inline关键字,这样做很不寻常。

Would a good compiler automatically inline this anyway? 一个好的编译器会自动内联这个吗?

Quite probably, yes, it would. 很可能,是的,它会。

Should I lose any sleep over this? 我应该失眠吗?

Better not. 最好别。 If you want to be super-sure (or you are super-curious), check the assembly that's going out from your compiler. 如果你想要超级肯定(或者你非常好奇),请检查从编译器发出的程序集。

Please note that shared_ptr is a class template , so its member functions are actually function templates . 请注意, shared_ptr是一个类模板 ,因此其成员函数实际上是函数模板

Since they are not export ed, they must not only be declared , but also defined in all translation units where they are used, just like a function defined with the inline storage specifier. 因为它们不是export主编,他们不仅要申报 ,但在使用它们,就像用定义的函数所有的翻译单位也定义 inline存储说明符。

In a way, template also means inline . 在某种程度上, template也意味着inline

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

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