Is it possible to use a reference as the value in a standard map container in C++?
If not – why not?
Example declaration:
map<int, SomeStruct&> map_num_to_struct;
Example usage:
...
SomeStruct* some_struct = new SomeStruct();
map_num_to_struct[3] = *some_struct;
map_num_to_struct[3].some_field = 14.3;
cout<<some_struct.some_field;
...
I would expect to see 14.3 printed…
No. STL container value types need to be assignable. References are not assignable. (You cannot assign them a different object to reference.)