c++ - Inline function linkage -


i can't make sense of following behavior: 1 header basic types, , header in use these types in several functions. afterward started constructing classes based on defined types , functions. in function header if leave following signature:

void whateverfunction(parameters) 

the linker points out there multiple definitions of whateverfunction. if change to:

inline void whateverfunction(parameters) 

the linkage problem gone , compiles , links well. know concerning inline replaces every function call it's code other it's pretty dark, question is:

how linker treats inline functions in c++?

when function in header not inline, multiple definitions of function (e.g. in multiple translation units) violation of odr rules.

inline functions default have external linkage. hence, consequence of odr rules (given below), such multiple definitions (e.g. in multiple translation units) okay:

$3.2/5- "there can more 1 definition of class type (clause 9), enumeration type (7.2), inline function external linkage (7.1.2), class template (clause 14), non-static function template (14.5.6), static data member of class template (14.5.1.3), member function of class template (14.5.1.1), or template specialization template parameters not specified (14.7, 14.5.5) in program provided each definition appears in different translation unit, , provided definitions satisfy following requirements. given such entity named d defined in more 1 translation unit, then

— each definition of d shall consist of same sequence of tokens; , [...]

how linker treats inline functions pretty implementation level detail. suffice know implementation accepts such mulitple defintions within limitations of odr rules

note if function declaration in header changed 'static inline....', inline function explicitly has internal linkage , each translation unit has it's own copy of static inline function.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -