Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #85779
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: map[string_view] not compiling |
| Date | 2022-08-01 17:11 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <tc8mtl$v3mr$1@dont-email.me> (permalink) |
| References | <tc8epl$t0hn$1@dont-email.me> <jkpvfaF1b1gU1@mid.individual.net> <Convertibility-20220801135655@ram.dialup.fu-berlin.de> |
01.08.2022 15:57 Stefan Ram kirjutas: > Bo Persson <bo@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).
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