4

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 ?
Community
  • 1
  • 1
ereOn
  • 53,676
  • 39
  • 161
  • 238

4 Answers4

11

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • Many thanks. Seems clearer to me now. What about `inline` and `template` functions ? – ereOn Jan 11 '11 at 09:25
  • @ereOn: function templates aren't functions but effectively behave as if they were `inline` in any case. Explicit specializations of functions can be `inline` independent of their original template. The same guidelines can be followed. – CB Bailey Jan 11 '11 at 09:30
  • I think this (and the possible duplicate I missed) answers all my questions. Thank you. – ereOn Jan 11 '11 at 09:34
3

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

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1

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.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • No, the linker does not need to reduce this to one instance. More correct would be to say, hopefully the function will be inlined (and have no symbol), if this fails it should have internal linkage. Whether it should be merged to one instance or not is a fully optional linker optimization. – Johan Kotlinski Jan 11 '11 at 09:23
  • It might be reduced to 0 instances in reality, but definitely not more than one, i.e. the ODR is preserved. – CashCow Jan 11 '11 at 13:19
0

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.

CrazyC
  • 1,840
  • 6
  • 39
  • 60