www.digitalmars.com         C & C++   DMDScript  

c++.stlsoft - stlsoft::frequency_map: semantic change

reply "Matthew Wilson" <matthew hat.stlsoft.dot.org> writes:
I'm considering changing the semantics of operator[] in
stlsoft::frequency_map to return 0, rather than throwing std::out_of_range,
if a given key does not exist in the map.

stlsoft::frequency_map<std::string>  fm;

fm.push("abc");
fm.push("abc");
fm.push("def");

assert(2 == fm["abc"]);
assert(1 == fm["def"]);
assert(0 == fm["ghi"]); // **

Currently, statement ** will throw an instance of std::bad_alloc

Does anyone have a problem with / an opinion about this projected change?

Cheers

Matt
Sep 04 2007
parent Jim B <jimbat users.sourceforge.net> writes:
Matthew Wilson Wrote:

 I'm considering changing the semantics of operator[] in
 stlsoft::frequency_map to return 0, rather than throwing std::out_of_range,
 if a given key does not exist in the map.
 
 stlsoft::frequency_map<std::string>  fm;
 
 fm.push("abc");
 fm.push("abc");
 fm.push("def");
 
 assert(2 == fm["abc"]);
 assert(1 == fm["def"]);
 assert(0 == fm["ghi"]); // **
 
 Currently, statement ** will throw an instance of std::bad_alloc
 
 Does anyone have a problem with / an opinion about this projected change?

imo it is an improvement to the semantics. 0 is never going to be valid for an element in the fm, so there's no problem. Jim
Sep 05 2007