Skip to main content
added 145 characters in body
Source Link

Originally inline was a very strong hint that calls to the function should be inlined.

But the only guaranteed effect of inline is to allow a function to be defined (effectively identically) in multiple translation units, e.g., that you place the definition in a header file.

Nowadays, some compilers are very keen on following the inlining hint, e.g. g++. And some compilers take it less seriously, e.g. Visual C++. But all have to abide by the guarantee.

It is unfortunate that these two meanings -- optimization hint and what we might call a linker level discardable definition -- reside with the same keyword, because it means that you cannot practically have one without the other.

It is also unfortunate that inline (or better, a separate keyword about discardable definition) cannot¹cannot be applied to data.

The need for linker level discardable data has increased as header-only modules have become more popular. E.g., many Boost sub-libraries are header-only.

For data you can, however, apply a little trick with templates. Define it in some class template, provide a typedef with template parameter void (or whatever). That's because the One Definition Rule makes a specific exception for templates.

Cheers & hth.,

*Notes:* ¹ `inline` variables will be [supported in C++17][1].

Originally inline was a very strong hint that calls to the function should be inlined.

But the only guaranteed effect of inline is to allow a function to be defined (effectively identically) in multiple translation units, e.g., that you place the definition in a header file.

Nowadays, some compilers are very keen on following the inlining hint, e.g. g++. And some compilers take it less seriously, e.g. Visual C++. But all have to abide by the guarantee.

It is unfortunate that these two meanings -- optimization hint and what we might call a linker level discardable definition -- reside with the same keyword, because it means that you cannot practically have one without the other.

It is also unfortunate that inline (or better, a separate keyword about discardable definition) cannot be applied to data.

The need for linker level discardable data has increased as header-only modules have become more popular. E.g., many Boost sub-libraries are header-only.

For data you can, however, apply a little trick with templates. Define it in some class template, provide a typedef with template parameter void (or whatever). That's because the One Definition Rule makes a specific exception for templates.

Cheers & hth.,

Originally inline was a very strong hint that calls to the function should be inlined.

But the only guaranteed effect of inline is to allow a function to be defined (effectively identically) in multiple translation units, e.g., that you place the definition in a header file.

Nowadays, some compilers are very keen on following the inlining hint, e.g. g++. And some compilers take it less seriously, e.g. Visual C++. But all have to abide by the guarantee.

It is unfortunate that these two meanings -- optimization hint and what we might call a linker level discardable definition -- reside with the same keyword, because it means that you cannot practically have one without the other.

It is also unfortunate that inline (or better, a separate keyword about discardable definition) ¹cannot be applied to data.

The need for linker level discardable data has increased as header-only modules have become more popular. E.g., many Boost sub-libraries are header-only.

For data you can, however, apply a little trick with templates. Define it in some class template, provide a typedef with template parameter void (or whatever). That's because the One Definition Rule makes a specific exception for templates.

*Notes:* ¹ `inline` variables will be [supported in C++17][1].
Source Link

Originally inline was a very strong hint that calls to the function should be inlined.

But the only guaranteed effect of inline is to allow a function to be defined (effectively identically) in multiple translation units, e.g., that you place the definition in a header file.

Nowadays, some compilers are very keen on following the inlining hint, e.g. g++. And some compilers take it less seriously, e.g. Visual C++. But all have to abide by the guarantee.

It is unfortunate that these two meanings -- optimization hint and what we might call a linker level discardable definition -- reside with the same keyword, because it means that you cannot practically have one without the other.

It is also unfortunate that inline (or better, a separate keyword about discardable definition) cannot be applied to data.

The need for linker level discardable data has increased as header-only modules have become more popular. E.g., many Boost sub-libraries are header-only.

For data you can, however, apply a little trick with templates. Define it in some class template, provide a typedef with template parameter void (or whatever). That's because the One Definition Rule makes a specific exception for templates.

Cheers & hth.,