Page 1 of 1

Force inline functions in C++

Posted: Tue Feb 24, 2009 2:01 pm
by Pikkolamakkia
Hi, is possible force the inline functions when I compile a source with gcc?

Ps. Sorry for my bad English.... I'm not English :)

Re: Force inline functions in C++

Posted: Tue Feb 24, 2009 2:05 pm
by neonek
Maybe -finline-functions ?

Re: Force inline functions in C++

Posted: Tue Feb 24, 2009 2:23 pm
by overburn
erm sorry for the possible noob question , but can't you just declare them "inline" and that's it? :)

Re: Force inline functions in C++

Posted: Tue Feb 24, 2009 2:32 pm
by Pikkolamakkia
So the compiler decides if he want expand them or use them as functions.... I want force the expanding

Re: Force inline functions in C++

Posted: Tue Feb 24, 2009 2:40 pm
by Martijn
You can force it with a gcc attribute:
static void inline __attribute__((always_inline)) yourFunction() ...

Re: Force inline functions in C++

Posted: Tue Feb 24, 2009 2:45 pm
by Pikkolamakkia
I must declare it as static?

Re: Force inline functions in C++

Posted: Tue Feb 24, 2009 6:35 pm
by Troy Martin
Well, no, static just tells the compiler/linker that that function can only be used in the file that it is defined and written in.

Re: Force inline functions in C++

Posted: Tue Feb 24, 2009 7:53 pm
by JohnnyTheDon
Its a good idea to prevent clashes if you're puting the function inside of a header file. Otherwise each object file could export this function, leading to multiple definition errors when you link.

Re: Force inline functions in C++

Posted: Tue Feb 24, 2009 11:54 pm
by Solar
In some cases, forcing the inlining isn't a good idea, so make sure you are aware of the implications, e.g. when using virtual functions. (Read Scott Meyers' "Effective C++"?)

Re: Force inline functions in C++

Posted: Wed Feb 25, 2009 2:07 pm
by Pikkolamakkia
In some cases, forcing the inlining isn't a good idea, so make sure you are aware of the implications, e.g. when using virtual functions.
Yes but if I had to write a for cycle, I'd prefare write it as inline function that a real function (isn't faster???)

Re: Force inline functions in C++

Posted: Thu Feb 26, 2009 2:15 am
by Solar
Simply declare the function 'inline' (and define it in the header file) and let the compiler decide whether to actually inline it or not. That's why 'inline' is merely a suggestion: The compiler knows more about the generated code than you do. (Usually.)

"Forcing" something is almost never a good idea.