Question Details

No question body available.

Tags

c++ visual-c++

Answers (2)

April 20, 2026 Score: 1 Rep: 38,044 Quality: Low Completeness: 70%

You don't need to use forceinline or alter code in any way to make compiler inline functions from across different translation units. Performing release build compiled with /GL (whole program optimization) /Ob2 (inline any suitable) and linked with /LTCG (link-time code generation) should be sufficient.

Slightly modified example from Chronial's answer. Difference between the address x and y is 8 bytes even though no forceinline is used: https://godbolt.org/z/da46oMx8n

From my experience compilers these days seem to be VERY eager to inline, so not only a need for forced inlining doesn't ever occur, sometimes i need to force compiler to avoid inlining.

April 20, 2026 Score: 0 Rep: 72,192 Quality: Low Completeness: 70%

You cannot meaningfully specify forceinline on a function defined in another compilation unit. Your free function is also not inlined, the compiler just doesn't complain about it.

You can see this for example by having a look at the address of local variables in your functions.

Your code: https://godbolt.org/z/sxrqs67e6 – you can see that the addresses of x and y are quite a bit apart – they are on different stack frames.

If you move the function to the header (where you need define functions that you want to forceinline), you can see that x and y are now right next to each other in the same stack frame – dobaz was inlined: https://godbolt.org/z/z7sTPMc6P