Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline functions and external linkage

Tags:

c++

In this answer https://stackoverflow.com/a/4193698/738811 it's written that "Inline functions by default have external linkage". However it's not possible by default to link against something what is inline. So what's the sense to say that inline functions have external linkage?

like image 542
scdmb Avatar asked May 05 '13 15:05

scdmb


2 Answers

The linkage of a name has nothing to do with where or how it is defined, just with where the name may be used to refer to a particular object or function.

Declaring a function inline does not force it to be inlined; it just relaxes the One Definition Rule to allow a definition in each translation unit in which it's used (and require one in each translation unit in which it's called), to make it easier to inline. It doesn't prevent a non-inline version being generated, if the compiler decides not to inline a particular call to it, or if you take the address of it.

So "external linkage" and "inline" are not exclusive; "external linkage" means that the function may be referred to in any translation unit, and "inline" means that it must be defined in any translation unit that calls it.

like image 61
Mike Seymour Avatar answered Nov 18 '22 21:11

Mike Seymour


Maybe a better way to express it is "If linkage is necessary, it will be external". Meaning, if you take the address of the inline function, that address will be externally visible (not a static to a module).

like image 1
Scott Jones Avatar answered Nov 18 '22 19:11

Scott Jones