What does 'inline' mean?

Walter Bright newshound2 at digitalmars.com
Sat Jun 13 00:54:18 UTC 2020


On 6/12/2020 5:17 PM, Andrei Alexandrescu wrote:
> 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.

They are not merged in D, for the simple reason that ModuleA.fun() and 
ModuleB.fun() will have different (mangled) names presented to the linker.

(Of course, if you mark them extern(C) or extern(C++) the mangling will be the 
same and so they will be merged.)

As I've mentioned previously, C++ has all kinds of contortions trying to make 
inline work in a universe with:

1. .h file inclusion
2. no modules
3. no COMDATs

D just defines these problems out of existence.


More information about the Digitalmars-d mailing list