Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #85781
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: map[string_view] not compiling |
| Date | 2022-08-01 23:23 +0200 |
| Message-ID | <jkquhoF6e5sU1@mid.individual.net> (permalink) |
| References | <tc8epl$t0hn$1@dont-email.me> <jkpvfaF1b1gU1@mid.individual.net> <Convertibility-20220801135655@ram.dialup.fu-berlin.de> <tc8mtl$v3mr$1@dont-email.me> <a0841755-8c9c-470f-9dc9-ad71c6030254n@googlegroups.com> |
On 2022-08-01 at 22:30, Öö Tiib wrote: > On Monday, 1 August 2022 at 17:11:16 UTC+3, Paavo Helde wrote: >> 01.08.2022 15:57 Stefan Ram kirjutas: >>> Bo Persson <b...@bo-persson.se> writes: >>>> Not an expert, but operator[] takes a Key parameter, which is >>>> std::string here. And string_view is not implicitly convertible to >>>> std::string. >>> >>> Even more not an expert, but I take it from the Web that >>> even convertibililty alone would not suffice. The Web says: >>> >>> |Three things are required for something like this to happen: >>> | >>> |1. The map's comparator must be a transparent comparator >>> | >>> |2. The at() method must have an overload that participates in >>> | overload resolution when the container has a transparent >>> | comparator. >>> | >>> |3. the parameter must be convertible to the map's key_type. >>> | >>> |Neither of these are true in your example. The default >>> |std::less comparator is not a transparent comparator, there >>> |is no such overload for at(), and std::string does not have >>> |an implicit conversion from std::string_view. >>> | >>> The World-Wide Web (2021). >>> >> >> Thanks, this is very clear for me now *how* it is not working. But I'm >> more interested in *why* is it not working. This would be a quite >> reasonable operation in my mind: >> >> 1. Look up the map element via a string_view. >> 2. If not found, only then create a string from string_view and insert >> it as a new element. >> >> As for now, it looks like I need to use a pessimization by creating a >> string object always (together with the copy and dynamic allocation >> penalties), or I need to devise some non-trivial workaround via >> lower_bound and insert(hint). > > Yes, the heterogeneous lookup of ordered containers was added by C++14, > but it was for things like find and lower_bound not for operator []. > C++17 added non-heterogeneous insert_or_assign(). > C++20 added heterogeneous lookup to unordered containers but also did > not touch those access functions. > Perhaps no one proposed it. > > The string_view is optimization and so it is livable that its usage does > not bring most elegant code. Also there have been controversy about > map operator[] before, maybe they did not want to risk with heterogenous > overload to it. One complication is that for find it is enough that a type is comparable to the key. For operator[] it would also have to be convertible to the key. For string and string_view this might be the same thing, but in general it is not.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
map[string_view] not compiling Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-01 14:52 +0300
Re: map[string_view] not compiling Bo Persson <bo@bo-persson.se> - 2022-08-01 14:32 +0200
Re: map[string_view] not compiling Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-01 17:11 +0300
Re: map[string_view] not compiling Öö Tiib <ootiib@hot.ee> - 2022-08-01 13:30 -0700
Re: map[string_view] not compiling Bo Persson <bo@bo-persson.se> - 2022-08-01 23:23 +0200
Re: map[string_view] not compiling Öö Tiib <ootiib@hot.ee> - 2022-08-01 22:52 -0700
Re: map[string_view] not compiling Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-04 08:31 -0700
Re: map[string_view] not compiling "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-03 11:34 +0200
Re: map[string_view] not compiling Juha Nieminen <nospam@thanks.invalid> - 2022-08-03 10:49 +0000
Re: map[string_view] not compiling "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-03 13:38 +0200
Re: map[string_view] not compiling Juha Nieminen <nospam@thanks.invalid> - 2022-08-04 06:16 +0000
Re: map[string_view] not compiling Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-03 14:43 +0300
Re: map[string_view] not compiling "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-03 13:56 +0200
csiph-web