You can still use range-for!
//std::vector<std::pair<TypeA, TypeB>> someInitializingFunction();
{
TypeA a;
TypeB b;
for (auto& p : someInitializingFunction()) {
std::tie(a, b) = p;
// do stuff;
}
}
Or const auto& p
if you don't need/want to modify p
.
UPDATE: With the above, you can also move the elements to the tied variables using std::move
for (auto& p : someInitializingFunction()) {
std::tie(a, b) = std::move(p);
// do stuff;
}
for which your proposed syntax may not handle well. A contrived example:
for (std::tie(a, b) : std::move(someInitializingFunction())) {}
// Note: std::move here is superfluous, as it's already an r-value
// (may also hinder some optimizations). Purely for demonstration purposes.
With that, you don't have the ability to move the values of the elements to the tied variables, as begin()
, end()
, etc. from an r-value container won't produce move iterators. (Well, yes you could adapt the container into something that returns move iterators, but that would be a whole new story)