What does 'inline' mean?
Steven Schveighoffer
schveiguy at gmail.com
Mon Jun 8 14:59:52 UTC 2020
On 6/8/20 4:42 AM, evilrat wrote:
> On Monday, 8 June 2020 at 06:14:44 UTC, Manu wrote:
>>
>> I think a first part of the conversation to understand, is that since
>> D doesn't really have first-class `inline` (just a pragma, assumed to
>> be low-level compiler control), I think most people bring their
>> conceptual definition over from C/C++, and that definition is a little
>> odd (although it is immensely useful), but it's not like what D does.
>>
>> 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 this definition; what inline REALLY means is that the function is not
>> placed in the binary where it is defined, it is placed in the binary
>> where
>> it is CALLED, and each CU that calls an inline function receives their
>> own
>> copy of the function. From here; optimisers will typically inline the
>> call
>> if they determine it's an advantage to do so.
>>
>
> Sorry for sticking in my nose, but in result inline in C++ simply tells
> the linker "this symbol could be present multiple times, just pick
> whichever one of them and done with it" so it won't complain about
> multiple symbols anymore.
> And that's why you have to slap inline to functions that have body in
> header in order to build.
>
> But I'm not very certain if that's all about it, not using C++ that much
> since then, and never to that truly deep extent where any loose bit can
> destroy performance, not beyond fixing the client's code and some crappy
> pet projects.
This was not true the last time I found "inline abuse".
At a previous company, there was a type where a function was inline or
not depending on whether you defined a macro. And when you defined that
macro, the implementation was DIFFERENT (and different in a way that
calling the non-inline version would be a bug).
Which meant that if the linker was free to choose whichever
implementation it wanted, it would defeat the purpose of the inline
mechanism (BTW, this was the first time I ever experienced an inline
function, and had to research what it meant).
I think Manu's description is accurate, but I also thought it actually
inlined the function call as well, even without an optimizer. Hard to
tell when the end result isn't obvious.
-Steve
More information about the Digitalmars-d
mailing list