Inlining, visual c++

Started by
9 comments, last by blizzard999 19 years, 3 months ago
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.
Advertisement
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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;
int c = add(a,b);
}

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.
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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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.
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.
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.
Harry.
Quote:Original post by HarryW
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.


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
Quote:Original post by deffer
Quote:Original post by HarryW
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.


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.

This topic is closed to new replies.

Advertisement