Swapping two pairs in a map with a unique_ptr

I'm having some trouble with this, I have a map of <string, unique_ptr> and an iterator pointing to the pair I want to swap with the first pair in the map.

this code snippet is probably the best chance I have, but it complains about constness

    std::pair<std::string, std::unique_ptr<zge::State>> tempState;
    tempState = std::make_pair (m_States.begin()->first, std::move(m_States.begin()->second));

    m_States.begin()->first = it->first;
    m_States.begin()->second = std::move(it->second);

    it->first = tempState.first;
    it->second = std::move(tempState.second);

this errors with

error: passing 'const std::basic_string<char>' as 'this' argument of 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&)[with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]' discards qualifiers [-fpermissive]|

    std::swap(m_States.begin()->first, it->first);
    std::swap(std::move(m_States.begin()->second), std::move(it->second));

this errors with

error: no matching function for call to 'swap(std::remove_reference<std::unique_ptr<zge::State>&>::type, std::remove_reference<std::unique_ptr<zge::State>&>::type)'|

std::swap(std::move(m_States.begin()), std::move(it));

this errors with

error: no matching function for call to 'swap(std::remove_reference<std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::unique_ptr<zge::State> > > >::type, std::remove_reference<std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::unique_ptr<zge::State> > >&>::type)'|
Last edited on
The value_type of a std::map<> is std::pair<const Key, T>. The key is not mutable.
thanks, I'll have to think of something else, this is the first time I've used a map and the [] operator doesn't act the same way I thought it would so swapping both the key and value doesn't make any sense.
What do you want to do?
I was trying to implement a state machine for SFML, I did actually get it working two hours ago after hacking away at it for 8 hours (first time using map, first time using abstract base classes, first time using virtual functions, first time using unique_ptr and std::move, first time using enums/class enums, overall I'm pretty happy)

but my non-existent design for it caused it to be useless when you have any more than 1 state, so I'll have another go at it today and tomorrow, and use exceptions instead of class enums, hopefully it will turn out usable.

I realize now there was no reason to swap states around
Last edited on
Topic archived. No new replies allowed.