Can I avoid copies when returning multiple values, while keeping my return type?

262 Views Asked by At

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?

2

There are 2 best solutions below

0
On

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

1
On

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.