compiler optimization of return value in VS 2010
c++, compiler-optimization, inline, optimization, return-value-optimization
Solution
Visual Studio can't inline functions that return objects with non-trivial destructors:
In some cases, the compiler will not inline a particular function for mechanical reasons. For example, the compiler will not inline:
- A function if it would result in mixing both SEH and C++ EH.
- Some functions with copy constructed objects passed by value when -GX/EHs/EHa is on.
- Functions returning an unwindable object by value when -GX/EHs/EHa is on.
- Functions with inline assembly when compiling without -Og/Ox/O1/O2.
- Functions with a variable argument list.
- A function with a try (C++ exception handling) statement.
http://msdn.microsoft.com/en-us/library/a98sb923.aspx
Problem
using VS 2010 with full optimization /Ox look at the following two function calls: ``` static string test1(const string& input) { return input; } static void test2(const string& input, string& output) { output = input; } ``` If I use the latter test2 then the function is always optimized out and the code inlined. However test1 is not inlined unless I turn off exceptions. Does anyone know why this is? Additionally I would expect the compiler to be able to do as an efficient a job in test1 as test2 if it uses return value optimization but it seems not to be doing that. This also is puzzling me. The reason I want to use the first function signature would be that I have two compilable versions of the function. I want to have the calling code always call test1 and when a certain compile flag is set I want it to append the input to a copy and return it, when the compile flag is not set I want it to get as close to being a no-op as possible.