State of the value to insert

General Tech Bugs & Fixes 3 years ago

4.87K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

Is it possible the value passed to insert is left in a moved from state after move insertion if insertreturns false?

#include 
#include <map>

#include 

struct less
{
    template< typename T >
    bool operator () (const std::shared_ptr<T> & lhs, const std::shared_ptr<T> & rhs) const
    {
        return *lhs < *rhs;
    }
};

int main() {
    using key_type = int;
    using value_type = int;
    using map_type = std::map<std::shared_ptr, std::shared_ptr, less>;
    map_type m;
    auto p = typename map_type::value_type{std::make_shared(1), std::make_shared(1)};
    if (!m.insert(p).second) {
        assert(false);
    }
    assert(p.first);
    assert(p.second);
    if (m.insert(std::move(p)).second) {
        assert(false);
    }
    assert(p.first);
    assert(p.second);
}

Is the behavior of the last two assertion implementation defined?

0 views
0 shares

profilepic.png
manpreet 3 years ago

 

From [map.modifiers/2] on std::map::insert, we have

template<class P>  
pair<iterator, bool> insert(P&& x);

[...]

Effects: The first form is equivalent to return emplace(std::forward

(x)).

So it's in std::map::emplace... from [associative.reqmts/8] (emphasis mine):

a_­uniq.emplace(args)

Effects: Inserts a value_­type object t constructed with std::forwardrgs>(args)... if and only if there is no element in the container with key equivalent to the key of t.

Hence, construction does not take place if there is already an object in the container that is associated with an equivalent key.


Let's verify with  from the Llvm implementation. In what follows, I've deleted some parts of the code to make it more readable. First, std::map::insert does this:

template <class _Pp, /* some SFINAE... */>
/* symbol visibility ... */
pair<iterator, bool> insert(_Pp&& __p)
    {return __tree_.__insert_unique(_VSTD::forward<_Pp>(__p));}

Let's go to __tree::insert_unique, then:

pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
    return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
}

Still not there... but in __tree::emplace_unique_key_args it comes:

/* Template, template, template... return value... template */
__tree</* ... */>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
{
    __parent_pointer __parent;
    __node_base_pointer& __child = __find_equal(__parent, __k);
    __node_pointer __r = static_cast<__node_pointer>(__child);
    bool __inserted = false;
    if (__child == nullptr)
    {
        /* Some legacy dispatch for C++03... */

        // THIS IS IT:
        __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
        __r = __h.release();
        __inserted = true;
    }
    return pair<
                                                    
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum