Home > Mobile >  Moving from pair/tuple elements via structured binding
Moving from pair/tuple elements via structured binding

Time:09-23

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;

CodePudding user response:

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);
  • Related