STL map containing references does not compile

It is illegal to store references in an stl container, because types must be copy constructible and assignable. References can not be assigned.

Exactly what operation causes the first error is implementation dependent, but I image that it is related to creating a reference and not assigning it immediately. The second error message looks like its actually related to building the map index.

If your type is small, you can copy it into the map, or if it is large consider using pointers instead, but remember that the container will not deallocate the objects for you, you’ll have to do that explicitly yourself.

This SO question might be of interest to you.

Leave a Comment