C++ Logo

std-proposals

Advanced search

Reasons for adding insert_return_type in associative containers

From: Boyarinov, Konstantin <konstantin.boyarinov_at_[hidden]>
Date: Tue, 23 Jul 2019 05:29:52 +0000
Hello,

I have a small question regarding insert_return_type member type in STL ordered and unordered associative containers.
Node handling mechanism introduced two new overloads for insert:

insert_return_type insert(node_type&& nh); (1)
iterator insert(const_iterator hint, node_type&& nh); (2)

where insert_return_type is defined as a struct with 3 members:

struct insert_return_type {
                iterator position;
                bool inserted;
                node_type node;
};

An overload (1) inserts a node owned by nh into the container and returns the following inserts_return_type:

* If the insertion succeeds

o position points to the inserted element

o inserted is true

o node is an empty node handle

* If the insertion fails

o position poins to the element in container with the key, equivalent to nh.key() (or nh.value() for sets)

o inserted is false

o node is a node handle, contains the previous value of nh.
In either case, nh is left in an empty state.

An overload (2) inserts a node owned by nh into the container and if the insertion fails nh retains ownership on the node.

So the question is why such inconsistence is needed?
Why an overload (1) cannot be defined as:

std::pair<iterator, bool> insert(node_type&& nh);

and returns

1. If the insertion succeeds

a. Pair where iterator points on the inserted element, bool is true

b. nh is left in an empty state

2. If the insertion fails

a. Pair where iterator points on the element in the container with the key equivalent to nh.key()

b. nh retains ownership on the node

What do you think?
Thank you in advance.


Best regards,
Konstantin Boyarinov



--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

Received on 2019-07-23 00:31:52