Boost logo

Boost Users :

Subject: Re: [Boost-users] [unordered_map] Trying to understand some errors
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2011-05-04 15:35:31


On Mon, May 2, 2011 at 22:04, Craig Longman <craigl_at_[hidden]> wrote:
>
> I'm using VS v9, just in case that matters in this case.
>
>     unordered_map< string, uint32_t > xrf1;
>     unordered_map< string, uint32_t >::const_iterator& iter = xrf1.find(
> "hello" );
>
> This is something I've used many times with the other containers (well,
> std::map at least), but here I'm getting a conversion error.
>

VS is letting you do something not standards-compliant. You're trying
to bind a temporary to a reference-to-nonconst, which is illegal.

Try to do the same thing in a non-VS compiler with std::map and you'll
see it fail:
    #include <map>
    #include <string>
    using namespace std;
    int main() {
        map< string, int > xrf1;
        map< string, int >::const_iterator& iter = xrf1.find( "hello" );
    }

In function 'int main()':
Line 7: error: invalid initialization of non-const reference of type
'__gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<std::pair<const
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, int> >, __gnu_debug_def::map<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int,
std::less<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::allocator<std::pair<const
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, int> > > >&' from a temporary of type
'__gnu_debug::_Safe_iterator<std::_Rb_tree_iterator<std::pair<const
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, int> >, __gnu_debug_def::map<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int,
std::less<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::allocator<std::pair<const
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, int> > > >'

~ Scott


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net