Reputation: 53
I want to define an inline function in c++. For example, in the header file of this function fun.h
I had
#ifndef FUN_H
#define FUN_H
void fun(int);
#endif // FUN_H
and in the source file fun.cpp
I had
#include "fun.h"
inline void fun(int a){
return ++a;
}
I know this is one way to define an inline function but I did see plenty of people saying that the implementation of an inline function should directly be in the header file fun.h
, which means the following pattern,
#ifndef FUN_H
#define FUN_H
inline void fun(int a){
return ++a;
}
#endif // FUN_H
However, I do not want to squeeze all the codes together so I coded like this,
#ifndef FUN_H
#define FUN_H
void fun(int);
inline void fun(int a){
return ++a;
}
#endif // FUN_H
Is this a correct style so that the function can be thought as an inline function?
Btw, someone told me that if I put the definition of an inline function in a source file, it may cause an “undefined reference to” problem when linking. I did encounter this problem once and the solution is to delete the "inline" keyword in the source file. However, there are still some functions that have their definitions in the source file with inline keywords. This functions did not trigger any linking errors so I am confused now about the right way to use the "inline" keyword.
Does anyone have some ideas on it?
Upvotes: 0
Views: 146
Reputation: 1382
You can get an overview from their official documentation of inline function in c++,
When to use -
Function can be made as inline as per programmer need. Some useful recommendation are mentioned below-
- Use inline function when performance is needed.
- Use inline function over macros.
- Prefer to use inline keyword outside the class with the function definition to hide implementation details.
Also it is worth mentioning from that same place,
Pros :-
...
- By marking it as inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining)
And
Cons :-
...
- When used in a header, it makes your header file larger with information which users don’t care.
What I do is, as the function is quite small (as it should be), include it in the header file like this
#ifndef FUN_H
#define FUN_H
inline void fun(int a) { return ++a; }
#endif // FUN_H
So you are not being overwhelmed by the function definitions and can read off the function without scrolling too much and also skipping the compilation problems as you have mentioned in your question.
Upvotes: 1
Reputation: 30494
The inline
keyword tells the compiler that the function can be defined in multiple compilation units so long as all of the definitions are exactly the same. That's it. Nothing more, nothing less.
What this means is that marking a function that's defined in a .cpp file inline
doesn't really do anything, unless you're planning to manually copy/paste that definition into multiple .cpp files (which you obviously shouldn't be doing).
As for having vs. not having a separate declaration: that's entirely a matter of style. I find it redundant, but some people like to forward-declare all of their functions so they never have to worry about defining them in the right order if some of the call others.
Upvotes: 2