[ptr_container] boost::ptr_map

ptr_map<>::insert(key, value) function is taking first argument by non-const reference. Is it missing 'const' there?

On Fri, 7 Oct 2005 17:43:44 +0000 (UTC), Thorsten Ottosen wrote
ptr_map<>::insert(key, value) function is taking first argument by non-const reference. Is it missing 'const' there?
No, its an exception-safety issue. if evaluating the first expression could throw, we could leak.
Sorry to be slow, but how does const impact that -- just to prevent automatic conversion of keys from throwing? Jeff

Jeff Garland wrote:
On Fri, 7 Oct 2005 17:43:44 +0000 (UTC), Thorsten Ottosen wrote
ptr_map<>::insert(key, value) function is taking first argument by non-const reference. Is it missing 'const' there?
No, its an exception-safety issue. if evaluating the first expression could throw, we could leak.
Sorry to be slow, but how does const impact that -- just to prevent automatic conversion of keys from throwing?
If that's the case; wouldn't a better solution be to make insert() a template and delay the conversion until after the pointer is protected? template<class K> std::pair<iterator,bool> insert( K const& key, value_type x ); -- Daniel Wallin

Thorsten Ottosen <nesotto@cs.aau.dk> writes:
ptr_map<>::insert(key, value) function is taking first argument by non-const reference. Is it missing 'const' there?
No, its an exception-safety issue. if evaluating the first expression could throw, we could leak.
Could you please explain what you're saying? On the surface, that sounds like a flawed rationale on many levels. The arguments to a function can be evaluated in any order -- they can even be interleaved. Taking a parameter by non-const reference does nothing to prevent the corresponding expression from throwing. Is this another example of false user protection? -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (5)
-
Alexey Bakhvalov
-
Daniel Wallin
-
David Abrahams
-
Jeff Garland
-
Thorsten Ottosen