开发者

inline and good practices [duplicate]

This question already has answers here: Closed 12 years ago.

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 - No开发者_StackOverflow社区thing 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 ?


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.


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! :-)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜