Jump to content
  • Advertisement
Sign in to follow this  
pisrael

Inline functions C++

This topic is 4380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I read that in some cases inline functions use to be faster the normal functions, but are there any restriction in their use? Can I implement polymorphic (virtual) functions as inline? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
There are a great deal of restrictions on what can be inlined. Also note that "inline" is just a "reccomendation", that the compiler often ignores. The pragma _forceinline will make the compiler inline more often, but if the code is not inlinable this will be ignored too. Look at your disassembly window in release mode to see if a function is actually getting inlined.

Generally the obvious rules are:

- Inline functions must be in .h file (edit - if they have global scope, functions that are local to a single CPP file can be inlined in that c file, but if they are used in multiple files they must be in the header). For C++ members they can be inside the class definition or outside.
- Inline function cannot be virtual or used as callbacks.


One way of having alot inlined functions without having a nightmare of .h dependencies is to use .inl files. These are C files that are included in header in release mode, but compiled as c files in debug mode (that way in debug mode you can edit them without recompiling your whole source tree).

Share this post


Link to post
Share on other sites
Quote:
Original post by pisrael
I read that in some cases inline functions use to be faster the normal functions, but are there any restriction in their use? Can I implement polymorphic (virtual) functions as inline?


Use a little logic :
inline means that the code is expanded in the caller code at compilation time. If the function is polymorphic with virtual keyword, that means that the actual code of the function is only known when the function is called, that means at runtime and that it could change from run to run depending on what object it is called on. So, No, you cannot expand the function at compile time based on this.

(Of course there are some corner cases where the type of the function is known at compile time so that is a possible optimization but shouldn't be considered the typical use of a virtual function)

LeGreg

Share this post


Link to post
Share on other sites
Quote:
Original post by griffin2000
- Inline function cannot be virtual...

Sure they can. They just can't be called virtually.

So long as the compiler is able to tell exactly what code is going to be executed, that function call is a candidate for inlining.

CM

Share this post


Link to post
Share on other sites
every good compiler does inlining on its own, so who gives a damn?

[Edited by - maximAL on July 21, 2006 3:01:39 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by maximAL
every good compiling does inlining on its own, so who gives a damn?


There are plenty of cases where it won't (I've had to use _forceinline on many occasions). But even if you do trust your compiler, the rules still apply, if it can't work out at compile time how to inline (beacause its in the CPP file or its being called virtually) then it won't get inlined.

Share this post


Link to post
Share on other sites
Quote:
Original post by griffin2000There are plenty of cases where it won't (I've had to use _forceinline on many occasions).

there are also occasions, where inlining is contra-productive. probably your compiler realized that.
did forcing inlining give you a noticable speedup?

[Edited by - maximAL on July 21, 2006 3:12:29 PM]

Share this post


Link to post
Share on other sites
I´m having the following problem:

In class GUI_Rectangle.h:


class GUI_Rectangle
{
public: //methods
bool intercepts(TScreenCoord p_x, TScreenCoord p_y);
};



in the class GUI_Rectangle.cpp:


inline bool GUI_Rectangle::intercepts(TScreenCoord p_x, TScreenCoord p_y)
{
return true;
}




In class GUI_Component.cpp, i have:


bool GUI_Component::intercepts(TScreenCoord p_x, TScreenCoord p_y)
{
bool v_intercepts = m_rectangle.intercepts(p_x, p_y);
return v_intercepts;
}



The compiler return me the error:


undefined reference to `GUI_Rectangle::intercepts(unsigned short, unsigned short)' GUI_Component.cpp


If I remove the inline keyword from GUI_Rectangle::intercepts it compiles ok. Any ideia in how to solve it?

Thanks all!


Share this post


Link to post
Share on other sites
Quote:
Original post by pisrael
I´m having the following problem:

The compiler return me the error:

undefined reference to `GUI_Rectangle::intercepts(unsigned short, unsigned short)' GUI_Component.cpp

If I remove the inline keyword from GUI_Rectangle::intercepts it compiles ok. Any ideia in how to solve it?

Thanks all!

The code for an inline function needs to be visible at the point where the function is called. When compiling a .cpp file, the only things that are visible are things in the .cpp file itself, as well as any files that have been #included, which usually just consists of .h files. The code for your inline function is in another .cpp file, which is not visible at the time of the function call. How can the compiler know what machine code to inline into the rest of the program at that point if it doesn't know the source code? That's the dilemma.

Two solutions:

1) Create an additional file, "GUI_Rectangle.inl", write the source code for your inline functions there, and at the bottom of "GUI_Rectangle.h", #include "GUI_Rectangle.inl". This will basically make everything in the .inl file a part of your .h file, so when "GUI_Component.cpp" #includes "GUI_Rectangle.h", it also grabs the code for your inline functions.

2) Just write the function definition write their in the class itself.

In class GUI_Rectangle.h:
class GUI_Rectangle
{
public: //methods
bool intercepts(TScreenCoord p_x, TScreenCoord p_y)
{
return true;
}
};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!