Help! Problem with inline functions and macros..

Started by
4 comments, last by Zahlman 15 years, 9 months ago
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.
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...
Yep, that's sorted it thanks Rob. :)
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
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
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 isinline 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?

This topic is closed to new replies.

Advertisement