I have this piece of code and I wonder if it is valid or can cause undefined behaviour:
#include <list>
#include <utility>
void myFunction(std::list<std::pair<int, int>> foo)
{
while (foo.size())
{
std::pair<int, int> const &bar = foo.front();
//work with bar
foo.pop_front();
}
}
I am using a reference to avoid duplicating the already existing pair.
On one side, I think this could be an undefined behaviour because I am removing a referenced element but, on the other side, I am not accessing the reference after removing it.
Is it valid?