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

Hi,
The STL has an implementation of std::map

On Sunday, July 18, 2010, alfC
Hi, The STL has an implementation of std::map
. 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

On Sun, Jul 18, 2010 at 4:48 AM, Dave Abrahams
On Sunday, July 18, 2010, alfC
wrote: Hi, The STL has an implementation of std::map
. 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

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.
participants (4)
-
alfC
-
Dave Abrahams
-
Emil Dotchevski
-
Mathias Gaunard