How to add a function definition in header file?
This can be achieved in 3 possible ways:
- Marking the function
inline
or
- Making the function
static
or
- Putting the functions in anonymous namespace.
What is the correct way to do so?
#1
i.e: Marking the function inline
is the correct way to this without breaking the One Definition Rule.
What is wrong with the other two appraoches?
In both #2
& #3
each Translation Unit will contain it's own version of the function, and the program will contain several different versions of the function thus leading to a increase in size of the generated binary.
i.e: For a static
function fun()
, &fun
will be different in each translation unit, and the program will contain N
different versions of the function.
Also, If the function contains static local variables then there will be N
different static local variables, one for each function instance.
How the first approach avoids this problem?
An inline
function has external linkage.
When you mark a function inline
the function will have the same address in all translation units. Also, Static locals and string literals defined within the body of an inline function are treated as the same object across translation units.
In short, a inline function will have the same address across all translation units.
What is the deal with static inline
function definitions in header?
The static
keyword forces the function to have a internal linkage.
Each instance of function defined as inline is treated as a separate function and each instance has its own copy of static locals and string literals. Thus, this would be similar to #2
.
Note:
The standard mandates that all definitions of inline
function in an user program You must have the exact same definition in all translation units in which the function is used or called.
Relevant Standerdese references:
C++03 Standard
3.2 One definition rule:
Para 3:
Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is used.
7.1.2 Function specifiers
Para 4:
An inline function shall be defined in every translation unit in which it is used and shall have exactly the same definition in every case (3.2). [Note: a call to the inline function may be encountered before its definition appears in the translation unit. ] If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required. An inline function with external linkage shall have the same address in all translation units. A static local variable in anextern inline function always refers to the same object. A string literal in an extern inline function is the same object in different translation units.