[Boost-users] [optional] boost improvement over std::map, the default initialization requirement

69 views
Skip to first unread message

alfC

unread,
Jul 18, 2010, 3:36:03 AM7/18/10
to boost...@lists.boost.org
Hi,
The STL has an implementation of std::map<T1, T2>. It has some
designs drawbacks in my opinion. For example, the fact that T2 has to
have a default copy constructor. Even if for T2, by design, doesn't
make sense to have a copy constructor.

The problem boils down to the fact that, in the STL design, if an key
element of the map is referenced with the operator[] and it doesn't
exist then an 'empty' (default) object T2 is created. In my opinion
this feature is independent of the other features of a map. For
example if the element doesn't exists and the context is not
assignment then it could give an exception.

One option would be to use std::map<T1, boost::optional<T2> >. But I
am not sure if that creates other problems. It at least creates the
problem of having to access elements with the ugly syntax (*m[key])

The question is, does boost have another more complete solution for
this problem?

(something that I would call optional_map<T1, T2>,
uninitializing_map<T1, T2>, elegant_map<T1,T2>)
Thank you,
Alfredo

BTW, This is a simple implementation that I imagine, although in my
opinion a clever implementation does not even need to use the
indirection of boost::optional, because the context of assigment
m[key]=val can be detected with template expressions, and only the
copy constructor is needed in this case.

(code not tested)
template<class T1, class T2>
class optional_map : protected std::map<T1, boost::optional<T2> >{
typedef std::map<T1, boost::optional<T2> > base;
public:
using std::map<T1, boost::optional<T2> >::size;
using std::map<T1, boost::optional<T2> >::empty;
T2& operator[](T1 const& key){return *base::operator[](key);}
T2 const& operator[](T1 const& key) const{return *base::operator[]
(key);}
};
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Dave Abrahams

unread,
Jul 18, 2010, 7:48:06 AM7/18/10
to boost...@lists.boost.org
On Sunday, July 18, 2010, alfC <alfredo...@gmail.com> wrote:
> Hi,
>  The STL has an implementation of std::map<T1, T2>. It has some
> designs drawbacks in my opinion. For example, the fact that T2 has to
> have a default copy constructor. Even if for T2, by design, doesn't
> make sense to have a copy constructor.
>
> The problem boils down to the fact that, in the STL design, if an key
> element of the map is referenced with the operator[] and it doesn't
> exist then an 'empty' (default) object T2 is created. In my opinion
> this feature is independent of the other features of a map. For
> example if the element doesn't exists and the context is not
> assignment then it could give an exception.

Easy solution: use find and insert instead of operator[]. HTH

--
--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Emil Dotchevski

unread,
Jul 18, 2010, 2:47:02 PM7/18/10
to boost...@lists.boost.org
On Sun, Jul 18, 2010 at 4:48 AM, Dave Abrahams <dave.a...@gmail.com> wrote:
> On Sunday, July 18, 2010, alfC <alfredo...@gmail.com> wrote:
>> Hi,
>>  The STL has an implementation of std::map<T1, T2>. It has some
>> designs drawbacks in my opinion. For example, the fact that T2 has to
>> have a default copy constructor. Even if for T2, by design, doesn't
>> make sense to have a copy constructor.
>>
>> The problem boils down to the fact that, in the STL design, if an key
>> element of the map is referenced with the operator[] and it doesn't
>> exist then an 'empty' (default) object T2 is created. In my opinion
>> this feature is independent of the other features of a map. For
>> example if the element doesn't exists and the context is not
>> assignment then it could give an exception.
>
> Easy solution:  use find and insert instead of operator[]. HTH

I haven't done any timings but isn't lower_bound+insert potentially
better because you can then pass the iterator from lower_bound to
insert as a hint?

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode

Mathias Gaunard

unread,
Jul 18, 2010, 2:53:45 PM7/18/10
to boost...@lists.boost.org
Le 18/07/2010 19:47, Emil Dotchevski wrote:
> I haven't done any timings but isn't lower_bound+insert potentially
> better because you can then pass the iterator from lower_bound to
> insert as a hint?

If you don't give a hint, insert calls lower_bound on its own. The hint
is only useful if you already know where it could be put.

Reply all
Reply to author
Forward
0 new messages