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

Moving from pair/tuple elements via structured binding


Given std::pair<std::set<int>, std::set<int>> p, what is the right syntax to move its elements via structured binding?

How to do

std::set<int> first_set  = std::move(p.first);
std::set<int> second_set = std::move(p.second);

via structured binding syntax? Is the following equivalent to the above?

auto&& [first_set, second_set] = p;

Solution

  • Is the following equivalent to the above?

    No, there is no move operation, only the member variable of p is bound to the lvalue reference first_set and second_set. You should do this:

    auto [first_set, second_set] = std::move(p);