Damir
Damir

Reputation: 56199

Is possible to separate declaration and definition of inline functions?

I need to define inline functions to improve performance of my code. At the moment declaration of functions are in .h file and definitions are in .cpp file. I added inline keyword at the front of each declaration of functions but I am getting link error. Is possible to separate declaration and definition of inline functions ?

Upvotes: 10

Views: 6848

Answers (7)

steffen
steffen

Reputation: 8968

Yes, but you have to put the implementation in the header file. That is because in order to be inlined, the definition has to be known, when including the header.

If you do so, modern compilers will automatically inline the function even without the inline keyword.

Upvotes: 3

Mike Stroven
Mike Stroven

Reputation: 1

I have found that in some embedded toolchains, the #inline keyword is not allowed in the function declaration. You can specify it in the definition, but (as stated above) many compilers ignore the keyword anyway.

Upvotes: 0

You can separate the declaration and definition fine, but that definition must be available in every translation unit that uses the function, e.g.:

#include <iostream>

inline void foo();

int main() {
  foo();
}

inline void foo() {
  std::cout << "Hi\n";
}

is perfectly legal and correct.


The exact quote from n3290 § 7.1.2.4 is:

An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [ Note: A call to the inline function may be encountered before its definition appears in the translation unit. —end note ]

Where § 3.2 basically says that it has to be identical everywhere, even overload resolutions etc.

Upvotes: 10

Andrzej
Andrzej

Reputation: 5127

Are you absolutely sure that making your functions 'inline' would improve your performance? I am pretty sure it will not.

The compiler is able to inline some function calls if and only if it can see the body of the inlined functions. So you need to include the body of the function as well, but if do it, you do not need to annotate your function with 'inline' because the compiler only needs the body of the function -- not your 'inline' keyword. Compilers nowadays are smart and know without your hints whether and when to inline functions. And inlining does not necessarily increase your program's performance, and it is likely to increase your executable's size.

See this article by Herb Sutter. He argues that keyword "inline" has no meaning in C++. But I disagree with him. Keyword "inline" makes one difference: you can specify the body of the inline function more than once in the program (provided that it is exactly the same definition each time) -- this is useful when putting function bodies in headers (if you need this for any reason).

Upvotes: 3

BxlSofty
BxlSofty

Reputation: 501

By definition, inline functions must be known at compile time. If you want to define them in a separate .h file, you can use a

#pragma once

trick to only insert their definition once.

Upvotes: 1

triclosan
triclosan

Reputation: 5714

You are need specify only once prototype or realization. Both is eligible.

Upvotes: 1

Kirill Kobelev
Kirill Kobelev

Reputation: 10557

Use a separate "implementation header" that you will still include everywhere?

Upvotes: 1

Related Questions