Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ inline functions: declare as such, define as such, or both? Why?

Tags:

c++

inline

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.

like image 842
HazySmoke Avatar asked Jun 27 '17 19:06

HazySmoke


1 Answers

True for member functions, and not explicitly-defined for non-member functions (I believe)

See §10.1.6 in ISO C++ std.

The inline specifier can be applied only to the declaration or definition of a variable or function

and

A function declaration (11.3.5, 12.2.1, 14.3) with an inline specifier declares an inline function.

It doesn't explictly state what will happen if an inline specifier only modifies the definition of a function.

What we can be sure of is that such member functions are guaranteed to be marked as inline (thanks to James Curran).

See §12.2.1.

An inline member function (whether static or non-static) may also be defined outside of its class definition provided either its declaration in the class definition or its definition outside of the class definition declares the function as inline or constexpr.

All three functions in GCC and non-member circumstance

As in GCC -O1 C++ mode, every function mentioned are inlined.

Code:

#include "stdio.h"

int foo();
inline int foo() {int i; for(i=0;i<100000;i++); return i+3; }

inline int bar();
int bar() {int i; for(i=0;i<100000;i++); return i+4; }

inline int foobar();
inline int foobar() {int i; for(i=0;i<100000;i++); return i+5; }

int foobar2();
int foobar2() {int i; for(i=0;i<100000;i++); return i+6; }

int main(){
    int a,b,c,d;
    a=foo();
    b=bar();
    c=foobar();
    d=foobar2();
    printf("%d %d %d %d", a, b, c, d);
}

Disassembly: IDA

We can see only foobar2 is called.

As in -O2 and -O3, inline doesn't matter so much. The compiler will decide by itself (in the case above, all 4 functions are inlined).

like image 62
Keyu Gan Avatar answered Oct 01 '22 21:10

Keyu Gan