69

I know that declaring a function (normal function not a method inside a class) as inline is a good practice when the function definition is small for performance and it save time for the compilation. But how about inline methods inside a class I don't understand the concept of inline methods inside a class? How to define them and how they work.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
AlexDan
  • 3,203
  • 7
  • 30
  • 46
  • 11
    "is a good practice when the funtion definition is small for performance and it save time for the compilation" - where did you read that? – Oliver Charlesworth Feb 21 '12 at 00:33
  • @OliCharlesworth : I mean if the function contains one or two statements then declaring this function as inline will save time of jumps that the compile do to the definition and specially if this funtion was called a lot of times. – AlexDan Feb 21 '12 at 00:39
  • 6
    Keep in mind that since the compiler is generating code for inlined functions at each call site, you probably aren't helping your compile time. Excessive inlining will degrade compile time and increase your executable size. – brendanw Feb 21 '12 at 02:19
  • 5
    Just a clarification. The inline declaration doesn't save time in compilation. It saves time during code execution at the expense of size. Let’s say that the code that you put in a function needs 500 byte. Without the inline the function use 500 byte but the cpu will “waste” time for the “jump” on the function. With the inline the code will use 500 Byte for each call but is faster. Basically the inline suggest the compiler to prefer speed over size. Really helpful in an embedded environment where the executing time is critical (i.e. interrupt) – Ramon La Pietra Oct 11 '17 at 07:34
  • The follow link is a short, but very good explanation from IBM about it. https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/inline_member.htm – TheArchitect Sep 14 '18 at 06:36

3 Answers3

69

but how about inline methods inside a class ?

Both syntaxes for inlining functions (using explicit inline and defining member-function inside class definition) provides only hint about inlining for compiler. From performance point of view, they are equal.

In case of defining a member-function inside a class declaration, the readability of the latter should be of your main concern: it really hurts to litter class interface with multiple line of implementation details. So avoid doing that if your member-function is more than one statement: return stuff or simple forwarding should be OK, but usually no more than that.

class MyClass
{
public:
    int f() const { return m_i; }
    int g() const;

private:
    int m_i;
};

inline int MyClass::g() const
{
    return m_i;
}

// both member-functions behave equally (except for naming)
nils
  • 2,424
  • 1
  • 17
  • 31
Alexander Poluektov
  • 7,844
  • 1
  • 28
  • 32
  • is the declaration and the definition of g() must be in the same file as the main() funtion. or it's possible to write just the declaration of the the funtion g() and the definition in another file. – AlexDan Feb 21 '12 at 01:18
  • 7
    The definition can be put in separate *file*, but it need to be in the same *translation unit* as the code that makes a call to `g()`. Which effectively means that the definition shall be included into source file containing that makes a call. – Alexander Poluektov Feb 21 '12 at 01:59
  • Does this mean that a function definition inside a class definition is automatically considered inline? If that's not the case, why is it possible to define multiple times the same class (with the same functions) across different files without breaking the functions' one definition rule? – Albert May 07 '20 at 08:46
  • @Hal Yes, it does – Alexander Poluektov May 17 '20 at 23:28
  • ' (except for naming)' does this mean naming of function after compilation? – kyb Mar 10 '21 at 06:10
28

Specifying a function/procedure as inline inside a class is hinting to the compiler that instead of creating code to call the function and pass parameters, the contents of the function should be placed at the point of call.

It can improve performance of the compiled binary when it becomes more efficient to execute the function without having to pass parameters. It can also be a detriment to performance because repeating the code that would have been in the function at every call location can cause bloat which lessens the liklihood that your code will be found in faster cache memory.

John
  • 6,433
  • 7
  • 47
  • 82
26

There are two options to offer to the compiler to make a class function inline:

(1) Defining a function in the declaration of the class (in a header file)

class Human {

public:

    Human(const char* name);
    Human();

    // is implicit inline
    void lookAt(const char* name) const {
        std::cout << "I'm looking at " << name << std::endl;

    }

private:
    char _name[30]; 

}; 

(2) Using the inline keyword explicitly in the definition of the function (in a header file)

    // is explicit inline 
    inline void lookAt(const char* name) const {
        std::cout << "I'm looking at " << name << std::endl;

    }
Jeinzi
  • 153
  • 3
  • 8
Jan Koester
  • 1,178
  • 12
  • 26
  • 14
    You can't put the the `inline` method in the .cpp file. It still has to go in a header. – KayEss Feb 21 '12 at 01:08
  • 9
    Yes, you are right. If I put the inline method definition in a .cpp file I'll get an "unresolved external" error from the linker. So whether explicit or implicit, the inline functions has to be in the header. – Jan Koester Feb 21 '12 at 01:18
  • 2
    There's an exception, though: *private* inline functions only used within the cpp file *can* be defined there... – Aconcagua Feb 21 '19 at 10:03