Search code examples
c++c++20stdtuplecopy-elisionrvo

Why do I not get guaranteed copy elision with std::tuple?


I would expect that in C++20 the following code prints nothing between prints of A and B (since I expect guaranteed RVO to kick in). But output is:

A

Bye

B

C

Bye

Bye

So presumably one temporary is being created.

#include <iostream>
#include <tuple>
struct INeedElision{
    int i;
    ~INeedElision(){
        std::cout << "Bye\n";
    }
};

std::tuple<int, INeedElision> f(){
    int i = 47;
    return {i, {47}};
}

INeedElision g(){
    return {};
}

int main()
{   
    std::cout << "A\n"; 
    auto x = f();
    std::cout << "B\n";
    auto y = g();
    std::cout << "C\n";
}

What is the reason for this behavior? Is there a workaround to avoid copy (without using pointers)?

https://godbolt.org/z/zasoGd


Solution

  • When constructing std::tuple<int, INeedElision> from {i, {47}}, the selected constructor of std::tuple takes elements by lvalue-reference to const.

    tuple( const Types&... args );
    

    Then when use {i, {47}} as the initializer, a temporary INeedElision will be constructed and then passed to the constructor of std::tuple (and get copied). The temporary object will be destroyed immediately and you'll see "Bye" between "A" and "B".

    BTW: The 3rd constructor of std::tuple won't be used for this case.

    template< class... UTypes >
    tuple( UTypes&&... args );
    

    It's a constructor template, and braced-init-list like {47} doesn't have type and can't be deduced by template argument deduction.

    On the other hand, if INeedElision has a converting constructor taking int, and make the initializer as {i, 47}, the 3rd constructor of std::tuple will be used and no temporary INeedElision is constructed; the element will be constructed in-place from the int 47.

    LIVE