# Inlining, visual c++

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

## Recommended Posts

Everytime I try to do simple inlines, the generated code is the same, no optimizations. Even code as simple as: inline long ScalarAdd(long x, long y) { return x+y; } generates the same code when used as: long ScalarAdd(long x, long y){ return x+y; } I've enabled all the optimization controls in the project settings, but it still never works. Am I doing something wrong, or is inline not actually suppose to "inline" it with the code.

##### Share on other sites
inline is a suggestion to the compiler, which is free to ignore it. It all depends on how the function is used.

If the function's body is not visible in the current translation unit, it cannot be inlined.
If the function has its address taken, it cannot be inlined.
etc.

##### Share on other sites
I realize it's only a suggestion, but why would something like

inline int add(int a, int b) { return a+b; }
void main(){
int a = 1;
int b = 2;
}

not be inlined by the compiler, the only reason i want to do this is rather than use so many defines, use the inline, but it's never actually inlining any code i write.

##### Share on other sites
What optimization options are you using?

When I turn optimizations on, g++ either just generates an empty main (if I don't use c), or directly replaces it with 3 (if I do use it).

##### Share on other sites
I'm not sure, maybe it doesn't show it's inlining it because I'm debugging it, I think it'll optimize it if i do release build.

##### Share on other sites
Yea, I tested it, it just doesn't optimize anything, including inlines, for debugging. Even though it gives you the option to inline functions for debugging.

##### Share on other sites
If you're debugging, the MSVC sort of assumes that you *want* to step through every function and thus won't inline. If you want to see if inlining is actually done you should look at the generated assemly listing of a release build.

##### Share on other sites
Fwiw, if you're using MSVC and you feel the need to force the compiler to inline, you can use something like this:

#ifndef FORCEINLINE#if (MSC_VER >= 1200)#define FORCEINLINE __forceinline#else#define FORCEINLINE __inline#endif#endifFORCEINLINE long ScalarAdd(long x, long y) {    return x+y;}

I'm unsure what happens with this if you try to do something that's not compatible with inlining, like take the address of the function, though. Presumably you'd get compile errors.

##### Share on other sites
Quote:
 Original post by HarryWI'm unsure what happens with this if you try to do something that's not compatible with inlining, like take the address of the function, though. Presumably you'd get compile errors.

AFAIK in MSDN they say even __forceinline is only a suggestion to compiler and it can be ignored if needed.

I think the problem lies in Debug mode. In Release, even while debugging (F10, F11), you can actually see where the function IS inlined, the debugger just don't follow F11 command but stays in place for a few instructions.

I always use Release in MSVC++, don't even touch Debug. I just find no benefits of it. I just have two configurations: Release_optimized and Release_NotOpt for debugging, so the code generated has same structure as original c++ code.

Cheers.
/def

##### Share on other sites
Quote:
Original post by deffer
Quote:
 Original post by HarryWI'm unsure what happens with this if you try to do something that's not compatible with inlining, like take the address of the function, though. Presumably you'd get compile errors.

AFAIK in MSDN they say even __forceinline is only a suggestion to compiler and it can be ignored if needed.

I think the problem lies in Debug mode. In Release, even while debugging (F10, F11), you can actually see where the function IS inlined, the debugger just don't follow F11 command but stays in place for a few instructions.

I always use Release in MSVC++, don't even touch Debug. I just find no benefits of it. I just have two configurations: Release_optimized and Release_NotOpt for debugging, so the code generated has same structure as original c++ code.

Cheers.
/def

To the OP: Can you show us your compiler switches? In MSVC, they should be under C/C++ -> Command Line. If your using the /Od switch, I'm pretty sure the compiler won't inline your functions.

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009489
• ### Who's Online (See full list)

There are no registered users currently online

×