Re: std::map, insert() issue
Quote:
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
Re: std::map, insert() issue
Quote:
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.
Quote:
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.