Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are class member functions inlined?

I think my question has been asked here before, I did read them but still little confused and therefore asking to make it clear.

The C++ standard says all member functions defined inside class definition are inline

I have also heard that compiler can ignore inlining of a function. Will that be true in the above case or it will be always inlined if defined inside class definition?

Also, what was the reason behind this design, making all functions defined inside class definition inline? And what inlining has to do with source and header files?

Update: So one should always define their functions outside class if not to be inlined, right?

Update 2 by JohnB: Two functions declared inside class definition could never call each other as they would have to each contain the whole body of the other function. What will happen in this case? (Already answered by Emilio Garavaglia)

like image 420
vidit Avatar asked Mar 16 '12 08:03

vidit


People also ask

What does it mean for a function to be inlined?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

How do you know if a function is inlined?

With gdb, if you cannot call to a function, one of its possible meanings is the function is inline. Flipping the reasoning, if you can call a function inside gdb, means the function is not marked inline.

Are class functions inline by default?

In fact, all the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline functions are also applied here. If you need to explicitly declare inline function in the class then just declare the function inside the class and define it outside the class using inline keyword. };

Are class member functions inline?

Member functions of a local class must be defined within their class definition. As a result, member functions of a local class are implicitly inline functions.


2 Answers

Confusion arises because inline has two effects:

  1. It tells the compiler that the function code can be expanded where the function is called, instead of effectively being called.
  2. It tells the compiler that the function definition can be repeated.

Point 1. is "archaic" in the sense that the compiler can in fact do what it likes in order to optimize code. It will always "inline" machine code if it can and find convenient to do and it will never do that if it cannot.

Point 2. is the actual meaning of the term: if you define (specify the body) a function in the header, since a header can be included in more sources, you must tell the compiler to inform the linker about the definition duplicates, so that they can be merged.

Now, by the language specification, free functions (not defined in class bodies) are by default not defined as inline, so defining in a header a thing like

void myfunc() {} 

if the header is included in more sources, then linked in a same output, the linker will report a multiple definition error, hence the need to define it as

inline void fn() {} 

For class members, the default is the opposite: if you just declare them, they will not be inlined. If you define them, they will be inline.

So a header should look like

//header file  class myclass { public:     void fn1()     {} //defined into the class, so inlined by default      void fn2(); };  inline void myclass::fn2() {} //defined outside the class, so explicit inline is needed 

And if myclass::fn2() definition goes into a proper source, must lose the inline keyword.

like image 167
Emilio Garavaglia Avatar answered Oct 09 '22 02:10

Emilio Garavaglia


The inline keyword has for a function 2 meanings:

  1. Code replacement: Wherever inline function is invoked, don't generate a function call for it but simply place the contents of the function at the place of its call (this is something similar to macro replacement, but type safe)
  2. One definition rule: Don't generate multiple definition for a inline function, only generate a single definition common for all (exception: static functions)

The 1st terminology ("Code replacement"), is simply a request to the compiler. which can be ignored as compiler is better to judge whether to put the text or a function call. (for example, virtual functions or recursive functions cannot be inlined).

The 2nd terminology ("One definition rule") is guaranteed to happen by any conforming compiler. This will generate only 1 definition for all translation units. This facility eases coder's work sometimes, as for smaller function one may not want to put its definition in .cpp file (e.g. getters, setters).
Moreover, for template function which are header only constructs, this effect is mandatory. Thus template functions are inline by default.

Examples:

class A { public:   void setMember (int i) { m_i = i; } }; 

In this example mostly compiler would suffice both terminologies

class A {   inline virtual ~A () = 0; }; A::~A() {} 

Here compiler can only suffice the 2nd requirement.

like image 33
iammilind Avatar answered Oct 09 '22 02:10

iammilind