Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #119374 > unrolled thread

std::map::emplace useful in any way?

Started byMarcel Mueller <news.5.maazl@spamgourmet.org>
First post2024-05-25 17:57 +0200
Last post2024-07-07 11:09 +0200
Articles 5 on this page of 25 — 7 participants

Back to article view | Back to comp.lang.c++


Contents

  std::map::emplace useful in any way? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2024-05-25 17:57 +0200
    Re: std::map::emplace useful in any way? Paavo Helde <eesnimi@osa.pri.ee> - 2024-05-25 20:37 +0300
      Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-05-28 17:53 +0200
        Re: std::map::emplace useful in any way? Paavo Helde <eesnimi@osa.pri.ee> - 2024-05-29 11:11 +0300
          Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-05-29 11:47 +0200
            Re: std::map::emplace useful in any way? Paavo Helde <eesnimi@osa.pri.ee> - 2024-05-29 15:05 +0300
              Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-05-29 18:56 +0200
                Re: std::map::emplace useful in any way? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-05-29 12:23 -0700
                  Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-05-30 16:01 +0200
    Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-05-25 19:41 +0200
      Re: std::map::emplace useful in any way? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-06-30 09:47 -0700
        Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-06-30 19:15 +0200
          Re: std::map::emplace useful in any way? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-06-30 11:46 -0700
          Re: std::map::emplace useful in any way? usenet@stegropa.de (Stefan Große Pawig) - 2024-06-30 21:21 +0200
            Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-06-30 23:49 +0200
              Re: std::map::emplace useful in any way? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-06-30 18:29 -0700
                Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-07-01 13:41 +0200
                  Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-07-07 13:17 +0200
                    Re: std::map::emplace useful in any way? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-07-07 09:16 -0700
                      Re: std::map::emplace useful in any way? Bonita Montero <Bonita.Montero@gmail.com> - 2024-07-07 18:18 +0200
              Re: std::map::emplace useful in any way? usenet@stegropa.de (Stefan Große Pawig) - 2024-07-01 07:41 +0200
    Re: std::map::emplace useful in any way? Sam <sam@email-scan.com> - 2024-05-25 18:29 -0400
    Re: std::map::emplace useful in any way? usenet@stegropa.de (Stefan Große Pawig) - 2024-06-30 21:09 +0200
    Re: std::map::emplace useful in any way? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-06-30 13:32 -0700
      Re: std::map::emplace useful in any way? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2024-07-07 11:09 +0200

Page 2 of 2 — ← Prev page 1 [2]


#119594

Fromusenet@stegropa.de (Stefan Große Pawig)
Date2024-07-01 07:41 +0200
Message-ID<87plrxvfd1.fsf@ID-208667.user.individual.de>
In reply to#119592
Bonita Montero <Bonita.Montero@gmail.com> writes:
> Am 30.06.2024 um 21:21 schrieb Stefan Große Pawig:
>> Bonita Montero <Bonita.Montero@gmail.com> writes:
>>> Am 30.06.2024 um 18:47 schrieb Andrey Tarasevich:
>>>> On 05/25/24 10:41 AM, Bonita Montero wrote:
>>>>>
>>>>> You can find() your key and if it doesn't exist you emplace() the
>>>>> new node.
>>>> Which is completely unacceptable, ...
>>>
>>> Depends on your constraints. Before C++17 there was no other solution
>>> for that and you had to accept that.
>> What about using lower_bound() and emplace_hint() instead? That is
>> available since C++11.
>
> lower_bound() needs a random-access iterator since it does a binary
> search.

I had std::map::lower_bound() in mind.

  Stefan

[toc] | [prev] | [next] | [standalone]


#119380

FromSam <sam@email-scan.com>
Date2024-05-25 18:29 -0400
Message-ID<cone.1716676183.465863.392201.1004@monster.email-scan.com>
In reply to#119374
Marcel Mueller writes:

> The emplace functions generally allows to construct container objects
> directly at the target location. But std::map expects a value type of
> std::pair<const Key, T>. Now the implementation of emplace needs to
> instantiate the value type just to get the key to find the insert
> location. This cannot happen at the target location, isn't it?
>
> I know, this has been solved in C++17 by try_emplace. I just wonder what
> was the idea of this function.

With a converting constructor for either the key or the value one could  
squeeze some lemon juice fairly easily out of std::map::emplace and avoid an  
occasional copy. But to make more lemonade one will then have to write a  
short, ugly novel using std::piecewise_construct, however that'll still  
generate unwanted copies.

std::map::try_emplace was just an attempt to avoid some of that gobbledygook  
and the copies, for the most common use cases. But, it should be possible to  
do anything with emplace that can be done with try_emplace. It just requires  
more sweat and tears, and maybe an extra copy. If the key and value  
constructors' arguments are simple types you might get lucky and avoid the  
extra copies from emplace().

[toc] | [prev] | [next] | [standalone]


#119588

Fromusenet@stegropa.de (Stefan Große Pawig)
Date2024-06-30 21:09 +0200
Message-ID<87jzi6gsct.fsf@ID-208667.user.individual.de>
In reply to#119374
Marcel Mueller <news.5.maazl@spamgourmet.org> writes:
> The emplace functions generally allows to construct container objects
> directly at the target location. But std::map expects a value type of
> std::pair<const Key, T>. Now the implementation of emplace needs to
> instantiate the value type just to get the key to find the insert
> location. This cannot happen at the target location, isn't it?
>
> I know, this has been solved in C++17 by try_emplace. I just wonder what
> was the idea of this function.

While the value type may be instantiated and immediately destroyed when
it turns out that an element with the same key already exists, emplace()
still allows you to insert values that are neither copyable nor moveable.

  Stefan

[toc] | [prev] | [next] | [standalone]


#119590

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2024-06-30 13:32 -0700
Message-ID<v5sfc1$lv3d$1@dont-email.me>
In reply to#119374
On 05/25/24 8:57 AM, Marcel Mueller wrote:
> The emplace functions generally allows to construct container objects
> directly at the target location. But std::map expects a value type of
> std::pair<const Key, T>. Now the implementation of emplace needs to
> instantiate the value type just to get the key to find the insert
> location. This cannot happen at the target location, isn't it?

Yes, it sorta-kinda can happen "at target location", If I correctly 
understand what you mean by "target location".

In mainstream implementations `emplace` does not really construct a some 
local `value_type t` object. Instead, it immediately allocates and 
constructs out of the supplied arguments a _node_ object. This node 
object is what the `std::map` is literally built of and, of course, that 
node object contains a `value_type` object inside.

If the key is not present in the map, the node object is immediately 
_attached_ directly into to the map. Otherwise, it is destroyed and 
deallocated. In other words, that node object is the "target location".

So, yes, there is an "optimistic" construction of a `value_type` object 
(which might prove to be a waste), but there's no subsequent 
copying/moving from that `value_type` object to the "target location". 
The `value_type` object is created as part of the "target location" from 
the very beginning.

This approach is what permits usage of `emplace` for insertion of 
non-copyable/non-movable objects into the map through 
`std::piecewise_construct` (which is likely what Stefan refers to in his 
comment).

-- 
Best regards,
Andrey

[toc] | [prev] | [next] | [standalone]


#119609

FromMarcel Mueller <news.5.maazl@spamgourmet.org>
Date2024-07-07 11:09 +0200
Message-ID<v6dlvn$2293e$1@gwaiyur.mb-net.net>
In reply to#119590
Am 30.06.24 um 22:32 schrieb Andrey Tarasevich:
> In mainstream implementations `emplace` does not really construct a some 
> local `value_type t` object. Instead, it immediately allocates and 
> constructs out of the supplied arguments a _node_ object. This node 
> object is what the `std::map` is literally built of and, of course, that 
> node object contains a `value_type` object inside.
[...]> This approach is what permits usage of `emplace` for insertion of
> non-copyable/non-movable objects into the map through 
> `std::piecewise_construct` (which is likely what Stefan refers to in his 
> comment).

Indeed. That is the point.


Marcel

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c++


csiph-web