I believe it is possible, with constraints. Since references aren't assignable at some later stage, you won't be able to call operator[] on the map. However, you can call various other member functions. As long as you don't break any rules for references. For example:
// You need the instances to exist before
auto a1 = SomeStruct();
auto a2 = SomeStruct();
auto a3 = SomeStruct();
// Creating the map with an initializer list.
std::map<int, SomeStruct&> map_num_to_struct = {
{ 1, a1 },
{ 2, a2 },
{ 5, a3 }
};
// The following won't work because operator[] returns
// a reference to the value, which can't be re-assigned.
// map_num_to_struct[6] = a1;
// These will work.
map_num_to_struct.insert({6, a1});
map_num_to_struct.insert(std::pair<int, SomeStruct&>(7, a1));
// Iterating through the map.
for (auto &a: map_num_to_struct) {
cout << a.first << ": " << a.second.some_field << endl;
}
// We can't use operator[] for indexing.
// map_num_to_struct[5].do_something();
auto a_iter = map_num_to_struct.find(5);
if (a_iter != map_num_to_struct.end()) {
cout << a_iter->first << ": " << a_iter->second.some_field << endl;
a_iter->second.some_field = 14.3;
cout << a_iter->first << ": " << a_iter->second.some_field << endl;
}
I don't know if the newer C++ standards have made this possible, but it works with GCC and clang at least.