Based on the answer here, It is only needed to define the method inside header file in order to make it inline. So my question is, why there is inline
keyword?

- 1
- 1

- 1,745
- 2
- 21
- 43
-
How about, look up `inline` in your favorite textbook or reference? Or even just google it. – Cheers and hth. - Alf Nov 02 '16 at 06:30
-
Are you asking whether it is redundant? i.e. if the compiler can figure things out (or the language can) then why have the keyword at all? – juanchopanza Nov 02 '16 at 06:42
-
It's unclear what you are asking, the fact that one says inlined methods has to be in the header does not bring to the question asking "why there is `inline` keyword?". Please clarify your question. – jpo38 Nov 02 '16 at 06:47
3 Answers
For example, if you want to define a free function inside a header (not a member function of a class), you will need to declare it inline
, otherwise including it in multiple translation units will cause an ODR violation.
The usual approach is to declare the function in the header and define it in a separately compiled .cpp
file. However, defining it in the header as an inline
function allows every translation unit that includes it to have its definition available, which makes it easier for that function to actually be inlined; this is important if the function is heavily used.

- 111,498
- 10
- 176
- 312
The historical reason for the inline
keyword is that older C compilers weren't as capable of optimising code as good quality modern C and C++ compilers are. It was therefore, originally, introduced to allow the programmer to indicate a preference to inline
a function (effectively insert the function body directly into the caller, which avoids overheads associated with function calls). Since there was no guarantee that a compiler could inline the function (e.g. some compilers could only inline certain types of functions) the inline
keyword was made a hint rather than a directive.
The other typical use of inline
is not as discretionary - if a function is defined in multiple compilation units (e.g. because the containing header is #include
d more than once) then leaving off inline
causes a violation of the one definition rule (and therefore in undefined behaviour). inline
is an instruction to the compiler (which in turn probably emits instructions to the linker) to resolve such incidental violations of the one-definition rule, and produce a working program (e.g. without causing the linker to complain about multiple definitions).
As to why it is still needed, functionally, the preprocessor only does text substitution ... such as replacing an #include
directive with contents of the included header.
Let's consider two scenarios. The first has two source files (compilation units) that do;
#include "some_header"
//some other functions needed by the program, not pertinent here
and
#include "some_header"
int main()
{
foo();
}
where some_header
contains the definition
void foo() { /* whatever */ }
The second scenario, however, omits the header file (yes, people do that) and has the first source file as
void foo() { /* whatever */ }
//some other functions needed by the program, not pertinent here
and the second as;
void foo() { /* whatever */ }
int main()
{
foo();
}
In both scenarios, assume the two compilation units are compiled and linked together. The result is a violation of the one definition rule (and, practically, typically results in a linker error due to multiply defined symbols).
Under current rules of the language (specifically, the preprocessor only doing text substitution), the two scenarios are required to be EXACTLY functionally equivalent. If one scenario was to print the value 42
, the so should the other. If one has undefined behaviour (as is the case here) so does the other.
But, let's say for sake of discussion, that there was a requirement that a function be magically inlined if it is defined in a header. The code in the two scenarios would no longer be equivalent. The header file version would have defined behaviour (no violation of the one definition rule) and the second would have undefined behaviour.
Oops. We've just broken equivalence of the two scenarios. That may not seem much, but programmers would practically have trouble understanding why one version links and the other doesn't. And they would have have no way of fixing that ... other than moving code into a header file.
That means, we need some way to make the two scenarios equivalent. This means there needs to be something in the code which makes them equivalent. Enter the inline
keyword, and prefix it to the definitions of foo()
.
Now, okay, one might argue that the preprocessor should do something a bit more intelligent i.e. do more than simple text substitution. But, now you are on a slippery slope. The C and C++ standards do (at length) specify that the preprocessor does that. And changing that would introduce a cascade of other changes. Whether that is a good idea or not (and, certainly, there is some advocacy for eliminating the preprocessor from C++ entirely), that is a much bigger change, with numerous ramifications on the language, and on programmers (who, whether it is good or bad, can rely on the preprocessor behaving as it does).

- 35,646
- 4
- 32
- 74
Short answer. It's not required. Compilers generally ignore the inline
keyword.
More comprehensive answers are already given in other questions, not to mention the second answer in the question you linked.

- 2,493
- 14
- 21
-
1" It's not required." is wrong. Can you think of something that is impossible without it? – Cheers and hth. - Alf Nov 02 '16 at 06:31
-
"More comprehensive answers are already given in other questions" — In that case, it would make sense to close this question as duplicate. Maybe you could supply a link (which would also help your answer, even if this question does not get closed as duplicate)? – Jonas Schäfer Nov 02 '16 at 06:35