Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there inline function declarations instead of inline function calls?

Tags:

c++

C++ (and various other languages) support inline functions. If I want a function to be inlined, I have to use the inline keyword in the declaration. To me, this seams pretty unintuitive: Why can't I just use inline when calling a function? Example:

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

instead of

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

This would allow me to inline the function in only specific places without having to duplicate the function. Also, every function could be inlined, which would make the mechanism much more flexible. Is there a reason for why this is not support?

like image 994
NyxMC Avatar asked Jan 24 '18 12:01

NyxMC


People also ask

What is inline function declaration?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory.

When might a compiler choose not to inline a function declared as inline?

The compiler can't inline a function if: The function or its caller is compiled with /Ob0 (the default option for debug builds). The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other). The function has a variable argument list.

What are inline functions How does the execution of inline functions differ from normal functions give the advantages of inline functions?

Inline functions provide following advantages: 1) Function call overhead doesn't occur. 2) It also saves the overhead of push/pop variables on the stack when function is called. 3) It also saves overhead of a return call from a function.

What is difference between inline function and non inline function?

Functions that are present inside a class are implicitly inline. Functions that are present outside class are considered normal functions.


2 Answers

Foreword:

To inline a function i.e. the act of inlining i.e. inline expansion is an optimization where a function call is replaced by duplicated set of instructions.

To declare an inline function is to declare to the linker that the function is to be treated differently than non-inline functions. Specifically, the one-definition-rule is different for inline functions. Inline functions are allowed to be defined in multiple translation units (and in fact required to be defined in all translation units where the function is odr-used). This is different from non-inline functions, which must be defined in exactly one translation unit. The inline keyword can be used the declare an inline function.

While inline declaration is named similarly to the inlining optimization, and while former can indirectly affect whether latter is possible, these two concepts are separate.

Now, to the answer.


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

You don't necessarily have to use the inline keyword. A call to a non-inline function can be expanded inline by the compiler if the function is defined in the same translation unit. Even that is not a requirement for a linktime optimizer which 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
}

That said, linktime optimization isn't always an option, and function calls do happen across translation units, so inline definition is in some cases indeed necessary to allow inline expansion.

A pedantic point: A function can declared inline without the inline keyword (if it is a member function).

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

There just isn't such syntax in the language.

Note that the function would still have to be defined in the same translation unit or else it cannot be expanded inline by the compiler. Thus the function would still need to be declared inline if you wanted inline expansion in more than one translation unit.

I suspect that the reason why there is no syntax to force inline expansion is probably the same as why there is no way to select which branch of an if statement is the "default" branch which might have marginal effect on performance (the reasoning is just my guess in both cases): Humans are notoriously bad at optimization. I see no technical reason why such language feature couldn't be introduced. But I wouldn't expect such feature to be very useful.

like image 106
eerorika Avatar answered Sep 21 '22 00:09

eerorika


In C++ inline does not mean that the calls should get inlined. It means that the definition is allowed to occur in multiple translation units, whereas normally only one definition is allowed.

Since header files are usually included in many translation units of the C++ program, any function definition in a header file will end up in multiple translation units. So, to make that work, you need the inline keyword for those definitions. It's still related to inlining though, since in this case (where the definition is in the header), the definition is available at all call sites that include the header, allowing the compiler to inline it, since it knows the full definition. If only a declaration would be available in the header, inlining would only be possible during linking (with 'link time optimization' aka LTO).

like image 36
Mara Bos Avatar answered Sep 18 '22 00:09

Mara Bos