Does guaranteed copying provide resolution with function parameters?

If I understood correctly, starting with C ++ 17, this code now requires that no copy be executed:

Foo myfunc(void) {
    return Foo();
}

auto foo = myfunc(); // no copy

Is this also true for function arguments? Will copies in the following code be optimized?

Foo myfunc(Foo foo) {
    return foo;
}

auto foo = myfunc(Foo()); // will there be copies?
+4
source share
2 answers

In C ++ 17, prvalues ​​("anonymous temporary") are no longer objects. Instead, they are instructions on how to build an object.

They can create temporary ones from their building instructions, but since there is no object there, there is no copy / move structure to execute it.

Foo myfunc(Foo foo) {
  return foo;
}

, foo prvalue myfunc. , "myfunc , foo". " " , .

auto foo = myfunc(Foo());

, Foo() - prvalue. : " a foo ()". myfunc. , , ().

myfunc.

myfunc foo. prvalue ( ) auto foo.

, foo (), auto foo.

++ 14 ++ 17, ( , ). return func_arg;.

+3

. cppreference:

- [...]:

  • , prvalue, cv-unqualified , ,

  • , return prvalue, prvalue.

, . -, foo myFunc Foo() (1 ), prvalue. , (. 1).

myFunc foo, , foo prvalue ( 2). , , foo - x. prvalue, foo ( myFunc), - - .

, . . .

+1

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


All Articles