I have the following code:
#include "stdafx.h"
#include <unordered_map>
#include <cassert>
int main()
{
struct Foo { int a; };
std::unordered_map<int, Foo> foos{ { 0, { 3 } }, { 1, { 4 } } };
for (auto &[i, foo] : foos)
{
foo.a = 6; //doesn't change foos[i].a
assert(&foo.a == &foos[i].a); //does not pass
}
auto &[i, foo] = *foos.begin();
foo.a = 7; //changes foo[0].a
assert(&foo.a == &foos[0].a); //passes
}
My question:
Why doesn't the first assert statement pass while the second passes?
Why can't I change the value of a foo
in the foos
map in a range-based for-loop?
Compiler: MSVS++17 Visual studio 15.3.2
Edit: The code now compiles if copy pasted into a visual studio project.
I posted a bugreport in VS and it is under investigation now.