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
   //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?

  • 2
    That would really depend on the compiler and possibly even the version... – Robbie Dee Jan 5 '17 at 13:10
  • 1
    Check the assembly code – Robbie Dee Jan 5 '17 at 13:14
  • 1
    @Robbie Dee, You would compare the object code than, in size I assume. Am I right? – user8469759 Jan 5 '17 at 13:16
  • 2
    That might not be entirely reliable - better to check the code itself. – Robbie Dee Jan 5 '17 at 14:02
  • 1
    I was under the impression that inline is just a suggestion to the compiler that a function can be inlined (loosely analogous to a macro), however it is up to the compiler to determine if it will be. – Robert Baron Jan 5 '17 at 14:52

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 {
  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).

  • 1
    I'm not sure I'm understanding here. But basically my coding style is the one said, i.e. all declarations in an header file and definitions in .cc file. My functions (long or short) are all defined in a .cc file. So I'm not sure what are trying to say and this would apply in my case. – user8469759 Jan 5 '17 at 13:56
  • 1
    And then given my style and what you said (your last sentence specifically) I shouldn't declare nothing as inline. Or moving the short functions in the header file for sake of shortness and readability followed by a further declaration as inline, but I still have the problem. – user8469759 Jan 5 '17 at 13:58
  • 1
    “You SHOULD define functions inline if they are small”: does this statement hold in cases where the function body calls other functions? – user8469759 Jan 5 '17 at 14:46
  • 3
    I think it's worth pointing out that the use of "inline" has evolved since it was first introduced. In a sense, it's like the "register" keyword - it was a request to the compiler by the programmer, which the compiler was free to ignore. However, the register keyword is now pretty much obsolete, and the inline keyword is now only used for its side-effects. Modern compilers will ignore a request to inline functions if they don't think it's useful, and will inline functions even if you didn't ask them to. – Simon B Jan 5 '17 at 16:19
  • 1
    @amon I'm reading through your link, but to be honest I think it's confusing. The very first line states that the specifier inline is used to declare inline functions. But later when it is explained what an inline function is the use of such a keyword is not mentioned at all... I'm really confused. It's like "inline specifier is used to declare inline functions" but later "an inline function doesn't need the keyword inline, but it has to have these features... (unless it's not static)". – user8469759 Jan 5 '17 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.