简体   繁体   中英

compiler optimization of return value in VS 2010

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.

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 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

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

This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object ( other than a function or catch-clause parameter ) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function's return value

—...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM