[ACCEPTED]-Inline function linkage-inline

Accepted answer
Score: 35

When the function in the header is not inline, then 32 multiple definitions of this function (e.g. in 31 multiple translation units) is a violation 30 of ODR rules.

Inline functions by default 29 have external linkage. Hence, as a consequence 28 of ODR rules (given below), such multiple 27 definitions (e.g. in multiple translation 26 units) are Okay:

$3.2/5- "There can be more 25 than one definition of a class type (Clause 24 9), enumeration type (7.2), inline function with external linkage (7.1.2), class 23 template (Clause 14), non-static function 22 template (14.5.6), static data member 21 of a class template (, member 20 function of a class template (, or 19 template specialization for which some 18 template parameters are not specified 17 (14.7, 14.5.5) in a program provided that 16 each definition appears in a different translation 15 unit, and provided the definitions satisfy 14 the following requirements. Given such 13 an entity named D defined in more than 12 one translation unit, then

— each definition 11 of D shall consist of the same sequence 10 of tokens; and [...]

How the linker treats 9 inline functions is a pretty much implementation 8 level detail. Suffice it to know that the 7 implementation accepts such mulitple defintions 6 within the limitations of ODR rules

Note 5 that if the function declaration in header 4 is changed to 'static inline....', then 3 the inline function explicitly has internal 2 linkage and each translation unit has it's 1 own copy of the static inline function.

Score: 8

The linker may not see inline functions 9 at all. They are usually compiled straight 8 into the code that calls them (i.e., the 7 code is used in place of a function call).

If 6 the compiler chooses not to inline the function 5 (since it is merely a hint), I'm not sure, but 4 I think the compiler emits it as a normal 3 non-inline function and somehow annotates 2 it so the linker just picks the first copy 1 it sees and ignores the others.

More Related questions