Jump to content
  • Advertisement
Sign in to follow this  
Jamie W

Help! Problem with inline functions and macros..

This topic is 3724 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 an inline function; extern inline long PGET_FUNC(int sh,int x1,int x2,int sub); inline long PGET_FUNC(int sh,int x1,int x2,int sub) { } So far so good, if I use rv=PGET_FUNC(..); it goes inline ok. I have a macro #define PGET(sh,x1,x2,sub,val) PGET_FUNC(sh,x1,x2,sub) Now, when I use PGET in code, e.g. rv=PGET(..); it works, but it isn't inline anymore. Why isn't it going inline? Is it possible to force it inline? (have tried __forceinline already). Any ideas? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Jamie W
I have an inline function;

extern inline long PGET_FUNC(int sh,int x1,int x2,int sub);

inline long PGET_FUNC(int sh,int x1,int x2,int sub)
{
}


Don't extern an inline function. The inline function should be just 'inline' and appear in a header - otherwise it cannot be inlined.

Quote:
Original post by Jamie W
So far so good, if I use rv=PGET_FUNC(..); it goes inline ok.


It shouldn't do... you've declared it extern.

I have a macro

Quote:
Original post by Jamie W
#define PGET(sh,x1,x2,sub,val) PGET_FUNC(sh,x1,x2,sub)

Now, when I use PGET in code, e.g. rv=PGET(..); it works, but it isn't inline anymore.


It is inlined, it's just that the inline func it's calling is not inlined...

Share this post


Link to post
Share on other sites
Quote:
Original post by RobTheBloke
Don't extern an inline function. The inline function should be just 'inline' and appear in a header - otherwise it cannot be inlined.


This is not entirely true. Some compilers, like Visual C++, can inline code between modules at link-time if you have the right optimizations on. Here's more about it: Inline Redux

Share this post


Link to post
Share on other sites
Also the compiler decides whether to expand the inline, or use it as a linked function. To explicitly expand a function, use the __forceinline key word (unless the compiler flags were set to not expand any inline).

This is because there are many cases where expanding an inline can cause worse performance instead. Often this is caused due to cache problems. But sometimes the compiler may be wrong when deciding (Oh... be carefull here, you REALLY must know what you're doing, many people think they do, when they don't)

Cheers
Dark Sylinc

Share this post


Link to post
Share on other sites
The compiler can apply inlining transitively. Now that you have the real problem addressed, you don't need a macro:


// or whatever the actual type of val is
inline long PGET(int sh, int x1, int x2, int sub, int val) {
return PGET_FUNC(sh,x1,x2,sub);
}

// Or even just give PGET_FUNC that extra parameter, with a default value,
// and ignore it:
inline long PGET_FUNC(int sh, int x1, int x2, int sub, int val = 0) {
// implementation
}


But why do you have all these calls in your code with a useless extra parameter, anyway?

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!