21

If you have a method and you want to give the compiler a hint that it is a good idea to inline it, you currently have two solutions. The first one is to define the methods when you declare your class:

class Vector {
private:
    double* data_;
    double* size_;
    double* capacity_;
public:
    double& operator[](int k) {
        return data_[k];
    }
    ...
}

As this method might reduce readability, another solution is to use the inline keyword and define the method out of class:

class Vector {
private:
    double* data_;
    double* size_;
    double* capacity_;
public:
    inline double& operator[](int k);
    ...
}

double& Vector::operator[](int k) {
    return data_[k];
}

This makes the code more readable (at least I prefer it). Reading my STL implementation, I found that they use a mix of the two. Some methods (those which I think should really be inlined) are defined in the class, and others are defined out of class with the inline keyword. The file also begins with a commented declaration of the class.

So my question is the following. Do current compilers (I am thinking of GCC, Clang, Intel, and Visual Studio) are more likely to inline a member function that is declared inside the class than a member function declared out of class with the inline keyword?

Remark: This question is not a duplicate of When should I write the keyword 'inline' for a function/method? as my question is about compiler implementations. Do these two ways of saying that you want those functions to be inlined are equivalent. The way the STL is written suggests that they are not.

Community
  • 1
  • 1
InsideLoop
  • 6,063
  • 2
  • 28
  • 55

3 Answers3

13

The inline keyword is considered a hint to compilers, however most compilers are much better at deciding what to inline than programmers so they usually ignore this hint.

The main (only?) use for the inline keyword nowadays it to allow functions to be defined in the header and not generate multiple definition link errors (which has nothing to do with inlining really).

Also please note that inlining happens at a function call site so it doesn't make sense to say a function is inlined since it may be inlined in some places and not in others (depending on the code around it).

My advice: use what you think is more readable since it will have no impact on actual inlining (unless you use something compiler specific like __forceinline (don't do that)).

Community
  • 1
  • 1
Motti
  • 110,860
  • 49
  • 189
  • 262
  • 1
    Thanks for pointing out the fact that a function is not inlined. A function call is the one that is inlined. But the C++ keyword inline is quite good at enforcing this confusion. – InsideLoop Mar 09 '15 at 10:43
  • Thanks for the hint about the usage of inline which do not give multiple definition link errors. But it still does not explain why STL implementors still use inline in templated classes. – InsideLoop Mar 09 '15 at 10:46
  • 2
    Template methods need to be visible at the point of use/instantiation, which means putting them in a header somewhere. So, the choice is just between defining a method literally inline in your class, or providing an `inline` declaration and defining it later (but still in a header). It's a stylistic choice, and nothing to do with your question on the likelihood of inlining at call sites. – Useless Mar 09 '15 at 11:29
  • @Motti: There's a difference between "inline functions" at the language level and functions that are inlined by the compiler. Methods that are defined within a class or with the inline keyword are "inline" methods to the compiler. Compiler often don't insert the code for such methods at the point of the call, but they may do so for methods that are not inline at all. A compiler could even partially inline (like an inline recursive factorial function might inline calls four levels deep, then call itself recursively). – gnasher729 Mar 09 '15 at 15:52
  • @gnasher729 sure, but historically the intent of the `inline` keyword was to inform the compiler that the developer wants this function to be inlined. Once compilers advanced sufficiently to make this redundant it remained (as @AndyBrown said) as a linker modifier. This is similar to how Herb Sutter says that `const` now means [thread safe](http://herbsutter.com/2014/01/13/gotw-95-solution-thread-safety-and-synchronization/). – Motti Mar 09 '15 at 19:52
  • At lest gcc still takes into account the presence of the inline keyword when making it's inlining decision. – MikeMB Dec 05 '16 at 08:09
6

Do current compilers (I am thinking of gcc, clang, Intel, Visual Studio) are more likely to inline a member function that is declared inside the class than a member function declared out of class with the inline keyword?

It makes absolutely no difference whatsoever.

As the others have noted, in a modern compiler inline is little more than a linkage modifier. Actual inlining is controlled by optimisation flags, linkage requirements and compiler-specific attributes.

Andy Brown
  • 11,766
  • 2
  • 42
  • 61
  • I makes sens that modern compilers ignore it. But why does the libc++ library has _LIBCPP_INLINE_VISIBILITY, defines some method in the class and others out of class? This seem to be a very complicated way of doing things. – InsideLoop Mar 09 '15 at 11:00
  • 1
    @InsideLoop -- That is due to the need to adjust the ELF visibility of inlines when building libc++ as a DSO so that you don't wind up with definitions from various shared objects that use libc++ clashing with each other at runtime. – LThode Mar 09 '15 at 12:54
  • At least gcc does listen to the inline keyword to some degree. Are you absolutely confident that the position of the function definition doesn't have any influence? – MikeMB Dec 05 '16 at 08:05
1

Methods defined inside the class definition are inline by default. Choosing one or another is, at most, minor hurdle for the compiler when it comes to optimization - it will most likely not matter which you choose.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625