[ptr_container] ptr_map throws in []

Hi. I dont see why ptr_map throws in operator[], I think it should have the same behavior of std::map and return a default constructed object in case it doesnt find the key. Anyway, it is just a opinion. Im using ptr_map in production code and Im obligated to use find to see if the key is already in the map, if it isnt. create one. -- Felipe Magno de Almeida UIN: 2113442 email: felipe.almeida at ic unicamp br, felipe.m.almeida at gmail com, felipe at synergy com I am a C, modern C++, MFC, ODBC, Windows Services, MAPI developer from synergy, and Computer Science student from State University of Campinas(UNICAMP). To know more about: Unicamp: http://www.ic.unicamp.br Synergy: http://www.synergy.com.br current work: http://www.mintercept.com

Hi Felipe, "Felipe Magno de Almeida" <felipe.almeida@ic.unicamp.br> wrote in message news:4239C1B1.3050507@ic.unicamp.br... | Hi. | | I dont see why ptr_map throws in operator[], I think it should have the | same behavior of std::map and return a default constructed object in | case it doesnt find the key. | Anyway, it is just a opinion Hm..yeah...I guess you can use map.at( "foo" ); if you want the exception to be thrown. I think you're right it should call insert a heap-allocated object. I will change this for the post-review. In the meantime, you can make a new function in ptr_map_adapter using lookup() as a template to get the behavior you need. . | Im using ptr_map in production code and I'm glad to hear that. -Thorsten

Ive modified the ptr_map_adapter in the operator[] to be: const_reference operator[]( const key_type& key ) const { //return lookup( key ); iterator i = find( key ); if( i != end() ) return *i; else { i = const_cast<ptr_map_adapter_base*>(this) ->insert(begin(), key, CloneAllocator::allocate_clone(value_type())); return *i; } } and the non-const just dont have the const_cast, but this way I think value_type is default-constructed and then copy-constructed isnt? well, this isnt very efficient, but to my needs it is already better. Thorsten Ottosen wrote:
Hi Felipe,
"Felipe Magno de Almeida" <felipe.almeida@ic.unicamp.br> wrote in message news:4239C1B1.3050507@ic.unicamp.br... | Hi. | | I dont see why ptr_map throws in operator[], I think it should have the | same behavior of std::map and return a default constructed object in | case it doesnt find the key. | Anyway, it is just a opinion
Hm..yeah...I guess you can use
map.at( "foo" );
if you want the exception to be thrown. I think you're right it should call insert a heap-allocated object.
I will change this for the post-review. In the meantime, you can make a new function in ptr_map_adapter using lookup() as a template to get the behavior you need. . | Im using ptr_map in production code and
I'm glad to hear that.
-Thorsten
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- Felipe Magno de Almeida UIN: 2113442 email: felipe.almeida at ic unicamp br, felipe.m.almeida at gmail com, felipe at synergy com I am a C, modern C++, MFC, ODBC, Windows Services, MAPI developer from synergy, and Computer Science student from State University of Campinas(UNICAMP). To know more about: Unicamp: http://www.ic.unicamp.br Synergy: http://www.synergy.com.br current work: http://www.mintercept.com

sorry, it's just wrong. Felipe Magno de Almeida wrote:
Ive modified the ptr_map_adapter in the operator[] to be: const_reference operator[]( const key_type& key ) const { //return lookup( key ); iterator i = find( key ); if( i != end() ) return *i; else { i = const_cast<ptr_map_adapter_base*>(this) ->insert(begin(), key, CloneAllocator::allocate_clone(value_type())); return *i; } } and the non-const just dont have the const_cast, but this way I think value_type is default-constructed and then copy-constructed isnt? well, this isnt very efficient, but to my needs it is already better.
Thorsten Ottosen wrote:
Hi Felipe,
"Felipe Magno de Almeida" <felipe.almeida@ic.unicamp.br> wrote in message news:4239C1B1.3050507@ic.unicamp.br... | Hi. | | I dont see why ptr_map throws in operator[], I think it should have the | same behavior of std::map and return a default constructed object in | case it doesnt find the key. | Anyway, it is just a opinion
Hm..yeah...I guess you can use
map.at( "foo" );
if you want the exception to be thrown. I think you're right it should call insert a heap-allocated object.
I will change this for the post-review. In the meantime, you can make a new function in ptr_map_adapter using lookup() as a template to get the behavior you need. . | Im using ptr_map in production code and
I'm glad to hear that.
-Thorsten
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- Felipe Magno de Almeida UIN: 2113442 email: felipe.almeida at ic unicamp br, felipe.m.almeida at gmail com, felipe at synergy com I am a C, modern C++, MFC, ODBC, Windows Services, MAPI developer from synergy, and Computer Science student from State University of Campinas(UNICAMP). To know more about: Unicamp: http://www.ic.unicamp.br Synergy: http://www.synergy.com.br current work: http://www.mintercept.com

"Felipe Magno de Almeida" <felipe.almeida@ic.unicamp.br> wrote in message news:423A06C2.6020404@ic.unicamp.br... | sorry, it's just wrong. | | Felipe Magno de Almeida wrote: | > Ive modified the ptr_map_adapter in the operator[] | > to be: | > const_reference operator[]( const key_type& key ) const | > { | > //return lookup( key ); | > iterator i = find( key ); | > if( i != end() ) | > return *i; | > else { | > i = const_cast<ptr_map_adapter_base*>(this) | > ->insert(begin(), key, | > CloneAllocator::allocate_clone(value_type())); | > return *i; | > } | > } Try iterator i = this->find( key ) if( i != this->end() ) return *i; else { auto_type ptr( new value_type ); this->c_private()[ key ] = ptr.get(); // strong return *ptr.release(); // nothrow } -Thorsten
participants (2)
-
Felipe Magno de Almeida
-
Thorsten Ottosen