The following code segment compiles with no problems, even though foo
is defined inline but not declared as such, and bar
is declared inline but not defined as such.
int foo();
inline int foo() { return 3; }
inline int bar();
int bar() { return 4; }
inline int foobar();
inline int foobar() { return 5; }
int main(){
// ...
}
My first question: does the compiler read foo
as inline or not? What about bar
? Is this specified by the C++ standard?
My second question: Which one of these is the best practice in declaring and defining inline functions? Is it foo
? bar
? or foobar
? Why?
inb4 I read some other posts related to this but none of them answer my question directly.
This answer seems to suggest that foo
is inline, but says nothing about bar
. It also doesn't explain why foo
is preferred over the others. This answer talks about when I should use inline functions. That's not my concern: I've already decided to use inline functions. My question (question 2, to be precise) is whether I should declare it as such, define it as such, or both, and why one of the conventions is better style than the rest. This question seems to be closer to my concern but nobody answered it.
{ return 3; }
will almost certainly be inlined, regardless of how you define/declare it. – Siestainline
on the definition or declaration – Resortfoo
andbar
areinline
with external linkage,foobar
is justinline
. – Fowliang