Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

madu

inline functions

This topic is 5386 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 have a function declared in a header, and it''s implementation declared in another cpp file. In the main file, i include the header, use the function, and everything works fine. But when I declare the function inline, both in the header and in the implementation, the liker says it can''t find it in the main file. What I am doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Declare and define the inline function in the header, and don''t put it in the source file.

Bad:

in.h

inline void somefunc();

in.cpp

inline void somefunc() {}


Good:

in.h

inline void somefunc() {}



Most compilers need the function body at compile-time to perform inlining. They don''t have it if it''s not in the header.

Share this post


Link to post
Share on other sites
Declaring functions in the .h file? But that''s so ugly! Is that the only way of making them inline?

Share this post


Link to post
Share on other sites
Personally I think declaring inline functions in the header looks neater, and even has a way of looking effecient. The whole point of using inline functions are for defining very short functions in a single line, for a little boost in performance. If you are using so much code that it makes your header file look ugly, than most likely, you shouldn''t be using inlines.

Share this post


Link to post
Share on other sites
I am not using much code, I am using many many relatively small functions. Some of them are 50% faster inline, and some are even almost 100% faster. Well anyway, there is performance gain by declaring them inline. They are heavily commented (the comments are 60% of the code file), so declaring them in the header file is a little nasty.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

The other thing to remember is that the inline keyword is more of a guideline to the compiler than an actual requirement. If you declare a function that is the compiler thinks is too big to inline then it will ignore the inline tag. Try to keep you inlined functions short and simple.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you can use __forceinline to makea function inlien nomatter what the compiler thinks of your code. also __fastcall can speed thigns up sometimes not sure when and when not to use that though.

Share this post


Link to post
Share on other sites

// Header
extern inline void somefunc();

// Source
inline void somefunc()
{
std::cout << "Do something..." << std::endl;
}


Edit: Just because something can be done, doesn't always mean it should be done.



DracosX:

Master of the General Protection Fault

[edited by - DracosX on September 22, 2003 11:58:16 PM]

Share this post


Link to post
Share on other sites
Yes, adding the keyword "extern" before "inline" in the header declaration makes the linker work. But there''s one small problem: it doesn''t make it inline anymore. I know this because I have one small function that runs exactly twice as fast with inline than without. Declaring the function in the header seems to work though. So this seems to be the only way. Thank you for your help.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!