Why are there built-in function declarations rather than built-in function calls?

C ++ (and other languages) support built-in functions. If I want the function to be inlined, I have to use the inline in the declaration. For me, this seam is pretty unintuitive: why can't I use inline when calling a function? Example:

 void foo(){...} inline foo(); 

instead

 inline void foo(...){...} foo(); 

This would allow me to embed the function only in certain places without duplicating the function. In addition, each function can be integrated, making the mechanism more flexible. Is there a reason why this is not support?

+5
source share
3 answers

Foreword:

Embedding a function, that is, an act of nesting, that is, a built-in extension, is an optimization where a function call is replaced by a duplicate set of instructions.

To declare an inline function, you must declare to the linker that the function must be handled differently than non-inline functions. In particular, one definition rule is different from built-in functions. Internal functions are allowed to be defined in several translation units (and in fact they must be defined in all translation units where the function is used by odr). This differs from non-line functions, which must be defined in only one translation unit. The inline can be used to declare an inline function.

While an inline ad is called similarly to inlining optimizations, while the former can indirectly influence whether the latter is possible, the two concepts are separate.

Now to answer.


If I want the function to be inlined, I have to use the inline in the declaration.

You do not have to use the inline keyword. A non-inline function call can be extended by the built-in compiler if the function is defined in the same translation unit. Even this is not a requirement for a link optimizer that sees all translation units.

Example:

 // a.cpp void foo(){} // a non-inline function inline void qux(){} // an inline function void bar(){ foo(); // this call to a non-inline function can be expanded inline // because the function is defined in the same TU } // b.cpp void foo(); // the non-inline function must not be defined // in multiple translation units inline void qux(){} // the inline function can and must be defined // in both translation units void xeb(){ foo(); // this call to a non-inline function can not be expanded inline by // the compiler because the function is not defined in the same TU // the call can be expanded by a linker qux(); // this call can be expanded by the compiler // because the function is defined in the same TU } 

However, link optimization is not always an option, and function calls are made through translation units, so a built-in definition in some cases is really necessary to enable built-in extension.

Pedantic point: a function can be declared inside a string without the inline (if it is a member function).

Why can't I use inline when calling a function?

There is simply no such syntax in the language.

Note that a function must still be defined in the same translation unit, otherwise it cannot be extended by the built-in compiler. Thus, a function must still be declared inline if you want an inline extension in more than one translation unit.

I suspect the reason for the lack of syntax for forced expansion inside is probably the same as why there is no way to choose which branch of the if is the default branch, which may have a slight effect on performance (reasoning is just my guess in both cases ): People are known to be poorly optimized. I see no technical reason why such a language function cannot be introduced. But I would not expect such a feature to be very useful.

+4
source

In C ++, inline does not mean that calls should be nested. This means that the definition is allowed to be performed in several translation units, while usually only one definition is allowed.

Since header files are usually included in many units of a C ++ program translation, any function definition in the header file will consist of several units of translation. So, to do this work, for these definitions you will need the inline . However, this is still related to the attachment, because in this case (where the definition is in the title), the definition is available on all sites containing the title, which allows the compiler to embed it because it knows the full definition. If only an ad is available in the heading, an attachment will be possible only when linking (with "link time optimization", as well as LTO).

+3
source

A short but sweet answer: "Inline" does not do what it is used to. These days, all of this means that the linker should ignore a lot of definitions from individual compilation units, but just pick one and go with it. The compiler can do anything. Most or all modern compilers ignore inline.

Bonus fact: in the latest and largest C ++, you can declare a variable as inline.

+1
source

Source: https://habr.com/ru/post/1274931/


All Articles