I keep hearing that the inline
no longer useful as a hint to the modern compiler, but is used to avoid multiple definition errors in a multi-source project.
But today I came across an example that the compiler obeys the keyword.
Without the inline
following code
#include <iostream> using namespace std; void func(const int x){ if(x > 3) cout << "HAHA\n"; else cout << "KKK\n"; } int main(){ func(5); }
using the g++ -O3 -S a.cpp
, generates assembly code with func
not nested.
However, if I add the inline keyword before the definition of func
, func
inserted into main
.
Part of the generated assembly code
.LC0: .string "HAHA\n" .LC1: .string "KKK\n" .text .p2align 4,,15 .globl _Z4funci .type _Z4funci, @function _Z4funci: .LFB975: .cfi_startproc cmpl $3, %edi jg .L6 movl $4, %edx movl $.LC1, %esi movl $_ZSt4cout, %edi jmp _ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_l .p2align 4,,10 .p2align 3 main: .LFB976: .cfi_startproc subq $8, %rsp .cfi_def_cfa_offset 16 movl $5, %edi call _Z4funci xorl %eax, %eax addq $8, %rsp .cfi_def_cfa_offset 8 ret .cfi_endproc
My compiler is gcc 4.8.1 / x86-64.
I suspect that the function may be included in the binding process, but I'm not sure if this will happen, and if so, how can I find out?
My question is why this piece of code seems to contradict modern guidelines, for example When should I write the keyword 'inline' for a function / method?