Optimize return value compiler in VS 2010

using VS 2010 with full optimization / Ox, see 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 latest test2, then the function is always optimized and the code is nested. However, test1 is not built in unless I turn off the exceptions. Does anyone know why this is?

Also, I expected the compiler to be able to perform as an efficient job in test1 like test2 if it uses return value optimization, but it doesn't seem to do that. It also puzzles me.

The reason I want to use the first signature of the function is because I have two compiled versions of the function. I want the calling code to always call test1, and when a particular compilation flag is set, I want it to add input to the copy and return it when the compilation flag is not set. I want it to be as close to -op as possible.

+6
source share
2 answers

Visual Studio cannot inline functions that return objects with non-trivial destructors:

In some cases, the compiler will not install a specific function for mechanical reasons. For example, the compiler will not be embedded:
  • Function, if this leads to mixing both SEH and C ++ EH.
  • Some functions with copied objects are passed by value when -GX / EHs / EHa is enabled.
  • Functions that return an expanded object by value when -GX / EHs / EHa is enabled.
  • Functions with built-in assembly when compiling without -Og / Ox / O1 / O2.
  • Functions with a variable argument list.
  • Function with try statement (C ++ exception).

http://msdn.microsoft.com/en-us/library/a98sb923.aspx

+8
source

The standard explicitly forbids the compiler to use return value optimization when the return value is a function parameter (12.8 / 31):

This permission of copy / move operations, called copying, is allowed in the following cases (which can be combined to eliminate multiple copies):

- in the return statement in a function with a return type of the class, when the expression is the name of a non-volatile automatic object ( except for the function or catch-clause parameter ), with the same cv-unqualified type as the return type of the function, the copy / move operation can be omitted, building an automatic object directly into the return value of the function

-...

+2
source

Source: https://habr.com/ru/post/912707/


All Articles