inline and good practices [duplicate]

本秂侑毒 提交于 2019-12-06 04:25:51

问题


Possible Duplicate:
When to use inline function and when not to use it ?

I have seen many source codes using different syntaxes regarding the inline directive.

namespace Foo
{
    class Bar
    {
        public:

            // 1 - inline on the declaration + implementation
            inline int sum1(int a, int b) { return a + b; }

            // 2 - inline on template declaration + implementation
            template <typename T>
            inline T sum2(T a, T b) { return a + b; }

            // 3 - Nothing special on the declaration...
            int sum3(int a, int b);
    };

    // 3 - But the inline directive goes after
    // In the same namespace and still in the header file
    inline int Bar::sum3(int a, int b) { return a + b; }
}

I failed to find an "official" guidelines regarding the usage of inline: I only know that inline is just a hint to the compiler and that it enforces internal linkage. I know not much about it.

Here are my questions:

  • Is (1) good practice ?
  • In (2), is the inline directive always needed ? (My guess would be "no", but I can't explain why). When is it needed ?
  • (3) seems to be the most used syntax. Is there anything wrong with it or should I use it too ?
  • Is there any other use (syntax) of inline I am unaware of ?

回答1:


No, and no! inline is not just a hint to the compiler and it doesn't enforce internal linkage.

inline is implicit on functions defined in a class body so you only need it on functions defined outside of classes. You should use it when, and only when, you need to enable the changes to the one definition rule that inline makes.




回答2:


With regard to your questions:

  1. This doesn't seem like a good usage because any member function implemented in the body of a class is implicitly marked inline. In other words, this particular inline declaration is redundant. That said, this function itself is a good candidate for inlining, since it's small, short, and probably uses more assembly instructions to call than to execute.

  2. The inline keyword isn't necessary here fir two reasons. First, the function is a member function implemented in the body of a class, so it's implicitly inline. Second, the function is a template, and you only need to mark functions defined in headers inline if they're non-template functions. However, this is actually a good candidate for an inline function for the reasons given above.

  3. This is a great candidate for an inline function and you're using the keyword perfectly here. This is a short function that could really benefit from it.

  4. Nope, that's all the syntactic uses. You seem to know what you're doing! :-)




回答3:


There is no need to use the inline keyword except when you are fully implementing a free-function in a header file. This instructs the linker that multiple (duplicate) symbols for this function should be allowed to exist, and the linker should reduce this to one instance.




回答4:


By default, if you declare and define a function inside the class it will be inline. Inline is request to the compiler and compiler decides whether it will be inline or not. In case of inline, the function body expanded in the code where it has been called. If function is big and has multiple loop and switch cases then compiler ignore the inline request and treat it as simple function. In simple way inline behavior is same as the preprocessors but the pros and cons are different.



来源:https://stackoverflow.com/questions/4655947/inline-and-good-practices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!