NyxCode
NyxCode

Reputation: 728

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

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?

Upvotes: 6

Views: 1628

Answers (3)

eerorika
eerorika

Reputation: 238341

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.

Upvotes: 7

Mara
Mara

Reputation: 1015

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).

Upvotes: 3

Jive Dadson
Jive Dadson

Reputation: 17026

Short but sweet answer: "Inline" does not do what it used to. These days, all it means is that the linker should ignore multiple definitions from separate compilation units, and just pick one and go with it. The compiler is free to do as it pleases. Most or all modern compilers ignore "inline".

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

Upvotes: 2

Related Questions