While doing code maintenance I found code like this:
auto networkEntry = _networkEntries[key];
networkEntry.port = port;
networkEntry.scope = scope;
The map data type used for _networkEntries
has two overloaded versions of the operator[]
:
template<class T>
class Map {
// ... simplified STD compatible container ...
T & Map::operator[](const Key & key);
const T Map::operator[](const Key & key) const;
};
The data type used in the map is a simple struct
.
Now I just wondered, the returned value for auto
could be a copy of the data structure, or a reference to the data structure. If a copy is returned, the assignments would not affect the stored values in the map.
I have three related question for this case:
- Can I know or test which version of the
operator[]
was used? - Which C++ rules do apply here?
- Is there a way, using
auto
, to make sure the reference is used?