Skip to main content
gamedev.net
🔒 Locked

C++ Inline functions

Started by ChopperDave Sep 1, 2006 at 8:21 PM 3 replies 950+ views
Original Post
ChopperDave
ChopperDave
Sorry if this may sound redundant but... Can someone give me a brief description of inline functions in C++? I know the keyword has something to do with making the compiler do something, but I'm not sure what. Also, when is it a good idea to make a function inline?
DrEvil
DrEvil
Inline basically tells the compiler to insert the contents of the function at the call site, avoiding the function call altogether. Typically only used with very small functions. Using them with large functions can bloat the code and result in slower code(if the compiler chooses to perform the inline, the keyword is a suggestion.)

Good candidates for inlining are many of the functions of a math library, vector library, accessors, modifiers, small functions called from a tight loop, and possibly functions you later identify as slow through profiling. Math/vector library especially due to their frequent use in alot of game code.

Inlining accessors for example, results in the same code as if the variable were public, but providing the compile time safety of having the accessor.
dbzprogrammer
dbzprogrammer
The compiler, when meeting a function tagged with the inline keyword, decides if it would be worthy of inlining to whenever it's called. If a fuunction is small, and is called frequently, the compiler will most likely allow the inline. It will take the lines of code and copy them to where each line the function was called. However, if it would be less expensive to the CPU to make the call to the function, rather than inline it, it won't inline it.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
ChopperDave
ChopperDave
Ah, ok. That makes sense.

Thanks for the replies.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.