What does 'inline' mean?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Jun 13 00:17:25 UTC 2020


On 6/12/20 2:08 PM, Johan wrote:
> On Friday, 12 June 2020 at 13:22:35 UTC, Andrei Alexandrescu wrote:
>> On 6/8/20 2:14 AM, Manu wrote:
>>> In C/C++, inline says that a function will be emit to the binary only 
>>> when it is called, and the function is marked with internal linkage 
>>> (it is not visible to the linker from the symbol table)
>>
>> By my recollection this is not the case for C++, at all.
>>
>> * "inline" does NOT change a function's linkage in C++. You may have 
>> inline functions with internal linkage (static inline) or (default) 
>> external linkage. This is important because e.g. defining a static 
>> variable in an extern inline function will have the same address in 
>> all calls to the function.
> 
> I believe Manu tried to explain that `inline` in C++ really only affects 
> how the linker must treat the symbol, and it is best to remember that it 
> does nothing at all concerning "inlining" (putting function body inside 
> another).

Thank you. That's not at all what he wrote. AT ALL. It's what I wrote.

I'm emphasizing this because it has been a recurring problem: a 
legitimate problem with an explanation lost in translation.

> `inline` is really a misnomer.
> The "linkage" effect of `inline` is required for C++ to work with 
> #include files. There is some room for confusion because, depending on 
> who is talking, the word "linkage" can have more nuances than just 
> internal/external.

It helps to use terminology that is well defined and standardized. 
That's where simply using the meaning defined and consistently used in 
the C++ standard document can be of great help.

> I personally take the broad stance of "linkage" 
> including anything the linker can do with the symbol.

Makes sense. It's really nice to just use 
http://eel.is/c++draft/basic.link, which ensures we all understand one 
another.

> Without `inline`, you would get multiple definition linker errors for 
> functions defined in a header file when linking together two translation 
> units that both included that header file. Instead of normal function 
> linkage that forbids multiple definition, `inline` changes linkage to 
> merge multiple definitions into one.

Not sure about that part - if linkage was static by means of using the 
"static" keyword, multiple definitions may not be merged. (I may be 
wrong, please correct me.) Consider:

static inline int fun() {
     static int x;
     return ++x;
}

In C++, each translation unit containing a definition of fun() will have 
a distinct address for x. I don't see how the bodies of those functions 
can be merged.

> In addition, emission of the function must happen in any translation 
> unit that references it (calling or address taken), and thus that 
> translation unit must also define it (in contrast to just declaring it). 
> I do not think `inline` forbids emission if the function is not referenced.

I also think that's the case. (Also not what was claimed.)


More information about the Digitalmars-d mailing list