Reputation: 131606
If we write the following function:
auto foo() {
Foo foo { /* ... */ };
do_stuff(foo);
return foo;
}
then NRVO should kick in, so that foo
does not get copied on return.
Now suppose I want to return two different values:
auto foo() {
Foo foo { /* ... */ };
Bar bar { /* ... */ };
do_stuff(foo, bar);
return std::make_tuple(foo, bar);
}
This naive implementation will likely trigger the construction of two copies of each Foo
and Bar
(GodBolt).
How should I best modify my code to avoid this copying, without messing up my return type?
Upvotes: 1
Views: 275
Reputation: 131606
This:
auto f() {
Foo foo;
Bar bar;
do_stuff(foo, bar);
return std::make_tuple(std::move(foo), std::move(bar));
}
will return an std::tuple<Foo,Bar>
(GodBolt); and will replace the copy construction by move construction (GodBolt).
Upvotes: 3
Reputation: 73081
Another way to do it would be to declare your Foo and Bar objects inside the tuple to begin with:
auto f() {
auto ret = std::make_tuple(Foo{...}, Bar{...});
do_stuff(std::get<0>(ret), std::get<1>(ret));
return ret;
}
That way you don't have to worry about moving them into a tuple because they are already in it.
Upvotes: 5