-
March 31st, 2014, 11:12 AM
#1
std::map, insert() issue
Is there a specific reason (and if yes, what) that std::map insert() does not have the Obvious (to me at least) override defined as:
Code:
some_return_type insert( const key_type& key, const mapped_type& mapped);
I'm not sure what the ideal return type should be in that case, I guess it could be the same as the one for insert with const value_type& (so returning a pair<iterator, bool>).
using the [] operator isn't always possible since that assumes/requires that there is a default constructor for mapped_type. (with an assortment of potential performance issues)
compare the Obvious (?)
Code:
foobarmap.insert(foo, bar);
vs. the somewhat more awkward
Code:
foobarmap.insert( std::pair<fooType,barType>(foo, bar));
this can be particularly annoying if the types are complex constructs of their own (even if you typedef it)
-
March 31st, 2014, 07:54 PM
#2
Re: std::map, insert() issue
Originally Posted by OReubens
Is there a specific reason (and if yes, what) that std::map insert() does not have the Obvious (to me at least) override defined as:
Code:
some_return_type insert( const key_type& key, const mapped_type& mapped);
Maybe because the key type and mapped types could go through a silent conversion, given you the other two argument constructor being called (which could lead to hard to find bugs even though the code would compile).
http://www.cplusplus.com/reference/map/map/insert/
For example:
Code:
std::map<char *, char*>
If your proposal of the two argument constructor were used, both char* arguments would silently convert into the (iterator, iterator) 2-arg constructor, and not the desired pair<char*, char*> that would be expected.
Regards,
Paul McKenzie
-
April 1st, 2014, 03:33 AM
#3
Re: std::map, insert() issue
Originally Posted by OReubens
this can be particularly annoying if the types are complex constructs of their own (even if you typedef it)
There is always std::make_pair(...) to prevent some repetition.
Originally Posted by Paul McKenzie
For example:
Code:
std::map<char *, char*>
If your proposal of the two argument constructor were used, both char* arguments would silently convert into the (iterator, iterator) 2-arg constructor, and not the desired pair<char*, char*> that would be expected.
I think in this case overload resolution would pick the non-template overload, so it would pick the (key_type, mapped_type) overload. However, with a std::map<std::string, std::string> and calling insert("key", "value"), I think the template (Iter, Iter) overload would be picked.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|