Subject: Re: [Boost-bugs] [Boost C++ Libraries] #924: Convenience function for const map read
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2007-07-03 15:15:39
#924: Convenience function for const map read
-------------------------------+--------------------------------------------
Reporter: nobody | Owner: nobody
Type: Feature Requests | Status: assigned
Milestone: | Component: None
Version: None | Severity: Showstopper
Resolution: None | Keywords:
-------------------------------+--------------------------------------------
Changes (by dave):
* severity: => Showstopper
Old description:
> {{{
> 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);
> }
>
> }}}
New description:
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);
}
}}}
--
Ticket URL: <http://svn.boost.org/trac/boost/ticket/924#comment:1>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:55 UTC