C ++ standard: return by copy to initialize a link without RVO: is there any copy?

Consider the following example:

struct big_type {};

// Return by copy
auto factory() { return big_type{}; }

void any_scope_or_function() {
    big_type&& lifetime_extended = factory();
}

Under the assumption that RVO is forbidden or not present at all and is in any way copied or can big_type()? Or will the link be directly related to the temporary construction in the expression return?

I want to be sure that the destructor big_typeis called only once when it finishes any_scope_or_function.

I use C ++ 14 if some kind of behavior has changed between the standard versions.

+4
source share
2 answers

Assuming no RVO / copy elison and then in

auto factory() { return big_type{}; }

big_type{} big_type. , . , , , .

big_type&& lifetime_extended = factory();

rvalue , , / .

,

auto factory() { return big_type{}; }

big_type factory() { return {}; }

factory. {},

+8

return?

, . , (N) RVO /, .

, , big_type, . : "big_type , any_scope_or_function ", , .

GCC/Clang : -fno-elide-constructors, (N) RVO, . , , .

+3

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


All Articles