Search code examples
c++c++17structured-bindings

structured bindings, reference ? Is it possible to remove them


Let's say I have such a tuple.

std::tuple<int &, int> tuple{};

I want to do something like that :

auto [i1, i2] = tuple; // Here i1 is lvalue reference, i2 is int

i1 is a lvalue reference because the first value on the tuple is one lvalue reference. However, I did not write auto &[i1, i2]. So, is there a possibility to "remove" the reference in this case? So that I got i1 and i2 as "simple" int. Thanks !


Solution

  • This structured binding is equivalent to:

    auto e = tuple;  
    auto&& i1 = e.get<1>();
    auto&& i2 = e.get<2>();
    

    Since tuple has type std::tuple<int&, int>, that is also the type of e.

    The structured binding syntax doesn't have a flavour to remove references from inside the tuple. However you can make a helper function to do that as in this question. Here is a working example:

    #include <tuple>
    #include <iostream>
    
    template <typename... T>
    using tuple_with_removed_refs = std::tuple<typename std::remove_reference<T>::type...>;
    
    template <typename... T>
    tuple_with_removed_refs<T...> remove_ref_from_tuple_members(std::tuple<T...> const& t) {
        return tuple_with_removed_refs<T...> { t };
    }
    
    int main()
    {
        int x{5}, y{6};
    
        std::tuple<int& , int> t(x, y);
    
        auto [i1, i2] = remove_ref_from_tuple_members(t);
    
        std::cout << i1 << ' ' << i2 << '\n';
        i1 = 7; i2 = 8;
        std::cout << x << ' ' << y << '\n';
    }
    

    Output:

    5 6
    5 6