C++ – When a function should be declared inline in C++

ccoding-style

I'm trying to rewrite some code I wrote time ago using some C++ good practices. To achieve such a goal I'm using as reference Effective C++ (1) and google coding convention (2). According to (2) a function should be declared inline if it is 10 lines or less, according to (1) furthermore the compiler could ignore the inline directive, for example when there are loops or recursion (just some example is provided so I don't know all the cases that would be ignored by the compiler).

Say I then have 2 functions, the first one is 10 lines, and there's no call to any other function and no external reference in general. The second one assume is still 10 lines but at some point there's a call to the first one

Something like

Type1 f(Type2 arg) {
   //10 lines of self contained code
}

Type3 g(Type4 arg) {
   //0 <= n <= 8 lines of code
   //g(x);
   //9 - n lines of code
}

I would declare the f inline, because of the suggestion given by google (fully justified) But I would be puzzled about g what would be a good practice here? Would declaring g as inline ignored by the compiler? If not can I still have the benefits of the inline directive?

Best Answer

In C++, the inline specifier does not affect whether a compiler will inline a function call. It has effects that make inlining possible, but that is not the main purpose. If you have a private helper function that you want to be inlined, instead ensure that it has internal linkage – e.g. by putting it in an anonymous namespace (C++11). A declaration with internal linkage can only be accessed from within the current compilation unit.

An inline function may have multiple definitions. This is important when you define a function inside a header file. Usually, you declare functions in a header and implement it in a .cpp file. Other compilation units include the declaration in the header and are later linked with the definition. But if the function is defined in a header, the same function now exists in multiple compilation units. When they are linked, the linker sees these multiple definitions for your function. Which is the correct definition? The linker will raise an error.

With an inline specifier, you tell the linker that these multiple definitions are OK, and they all refer to the same function. Additionally, the definition of an inline function must be present in the same compilation unit.

Some functions are implicitly inline – in particular, any methods and functions within a class declaration:

class Foo {
public:
  void method() { /* implicitly inline */ }
  static void static_method() { /* implicitly inline */ }
  friend void friend_function() { /* implicitly inline */ }
};

void free_function() { /* not implicitly inline */ }

Don't declare a function as inline because it is short. Declare it as inline because you've defined it in a header.

Further reading: inline specifier on cppreference.com

The Google style guide you cite does not discuss when the inline keyword should be used, but whether functions should be implemented inline (i.e. in the header file). For simple functions like getters in a class this is usually perfectly fine. For larger, more complicated functions, it is usually better to implement them separately (though this is not possible for templates).