Force inline functions in C++

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Pikkolamakkia
Posts: 8
Joined: Tue Feb 24, 2009 1:51 pm

Force inline functions in C++

Post 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 :)
neonek
Member
Member
Posts: 38
Joined: Thu Aug 28, 2008 1:53 pm
Location: Białystok - Podlasie, Poland

Re: Force inline functions in C++

Post by neonek »

Maybe -finline-functions ?
Please correct my English. If you'll find mistake please tell me about it so I can improve my English.
User avatar
overburn
Member
Member
Posts: 50
Joined: Sun Feb 22, 2009 9:15 am

Re: Force inline functions in C++

Post by overburn »

erm sorry for the possible noob question , but can't you just declare them "inline" and that's it? :)
One Tequila, Two Tequila, Three Tequila, Floor!
Pikkolamakkia
Posts: 8
Joined: Tue Feb 24, 2009 1:51 pm

Re: Force inline functions in C++

Post by Pikkolamakkia »

So the compiler decides if he want expand them or use them as functions.... I want force the expanding
Martijn
Posts: 22
Joined: Tue Feb 26, 2008 3:43 am
Location: The Netherlands

Re: Force inline functions in C++

Post by Martijn »

You can force it with a gcc attribute:
static void inline __attribute__((always_inline)) yourFunction() ...
Pikkolamakkia
Posts: 8
Joined: Tue Feb 24, 2009 1:51 pm

Re: Force inline functions in C++

Post by Pikkolamakkia »

I must declare it as static?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Force inline functions in C++

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Force inline functions in C++

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Force inline functions in C++

Post 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++"?)
Every good solution is obvious once you've found it.
Pikkolamakkia
Posts: 8
Joined: Tue Feb 24, 2009 1:51 pm

Re: Force inline functions in C++

Post 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???)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Force inline functions in C++

Post 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.
Every good solution is obvious once you've found it.
Post Reply