Boost C++ Libraries: Ticket #924: Convenience function for const map read https://svn.boost.org/trac10/ticket/924 <p> 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: </p> <pre class="wiki">template &lt;class Map&gt; typename Map::data_type const &amp; map_get(Map const &amp; 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-&gt;second); } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/924 Trac 1.4.3 Dave Abrahams Tue, 03 Jul 2007 15:15:39 GMT description changed; severity set https://svn.boost.org/trac10/ticket/924#comment:1 https://svn.boost.org/trac10/ticket/924#comment:1 <ul> <li><strong>description</strong> modified (<a href="/trac10/ticket/924?action=diff&amp;version=1">diff</a>) </li> <li><strong>severity</strong> → <span class="trac-field-new">Showstopper</span> </li> </ul> Ticket Dave Abrahams Tue, 03 Jul 2007 15:24:19 GMT status, version, severity changed; milestone set; owner deleted https://svn.boost.org/trac10/ticket/924#comment:2 https://svn.boost.org/trac10/ticket/924#comment:2 <ul> <li><strong>owner</strong> <span class="trac-author">nobody</span> removed </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">new</span> </li> <li><strong>version</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">release 1.34.0</span> </li> <li><strong>severity</strong> <span class="trac-field-old">Showstopper</span> → <span class="trac-field-new">Optimization</span> </li> <li><strong>milestone</strong> → <span class="trac-field-new">To Be Determined</span> </li> </ul> Ticket Dave Abrahams Fri, 13 Jul 2007 17:12:16 GMT status, resolution changed https://svn.boost.org/trac10/ticket/924#comment:3 https://svn.boost.org/trac10/ticket/924#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">invalid</span> </li> </ul> <p> 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 <a class="ext-link" href="http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?WantedLibraries"><span class="icon">​</span>http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?WantedLibraries</a> </p> Ticket