#in...">

Why are structured bindings in a range-based loop a copy, not a link?

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 is:

Why does the first approval command fail while the second passes? Why can't I change the value of a foo on the foos map in a for-loop?

Compiler: MSVS ++ 17 Visual studio 15.3.2

Edit: Now the code compiles if copying is pasted into a visual studio project.

+5
source share
1 answer

I sent an error message to VS and is now under investigation.

+1
source

Source: https://habr.com/ru/post/1271278/


All Articles