std::map, insert() issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: std::map, insert() issue

  1. #1
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,762

    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)

  2. #2
    Join Date
    Apr 1999
    Posts
    27,423

    Re: std::map, insert() issue

    Quote Originally Posted by OReubens View Post
    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

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,011

    Re: std::map, insert() issue

    Quote Originally Posted by OReubens View Post
    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 View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center