Opened 21 years ago

Closed 15 years ago

#924 closed Feature Requests (invalid)

Convenience function for const map read

Reported by: nobody Owned by:
Milestone: To Be Determined Component: None
Version: Boost 1.34.0 Severity: Optimization
Keywords: Cc:

Description (last modified by Dave Abrahams)

It's really awkward to use an STL map when you have only a const reference to it, because you cannot use operator[] -- you have to use find, check if the value was found, etc. (The reason being that operator[] creates a new map entry initialized with the null value if it doesn't find the key.) It would be much more convenient to have a lookup convenience function that either throws an exception if the key doesn't exist, or returns a default-initialized value. The version that returns a default value if the key doesn't exist might look something like this:

template <class Map>
typename Map::data_type const &
map_get(Map const & m, typename Map::key_type key)
{
  static typename Map::data_type default_value;
  typename Map::const_iterator it = m.find(key);
  return (it == m.end() ? default_value : it->second);
}

Change History (3)

comment:1 by Dave Abrahams, 15 years ago

Description: modified (diff)
Severity: Showstopper

comment:2 by Dave Abrahams, 15 years ago

Milestone: To Be Determined
Owner: nobody removed
Severity: ShowstopperOptimization
Status: assignednew
Version: Nonerelease 1.34.0

comment:3 by Dave Abrahams, 15 years ago

Resolution: Noneinvalid
Status: newclosed

Unless this suggestion applies to a particular library, it can't be assigned to anyone and is therefore not a valid ticket. Closing for now; it should be brought up on one of the Boost mailing lists and/or on http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?WantedLibraries

Note: See TracTickets for help on using tickets.