# Help! Problem with inline functions and macros..

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

## 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 on other sites
Quote:
 Original post by Jamie WI 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 WSo 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 on other sites
Yep, that's sorted it thanks Rob. :)

##### Share on other sites
Quote:
 Original post by RobTheBlokeDon'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 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 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 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?

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
13
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013658
×