From: "Daniel Krügler" <daniel.kruegler@gmail.com>
To: sotrdg sotrdg <euloanty@live.com>
Cc: "libstdc++" <libstdc++@gcc.gnu.org>
Subject: Re: transparent proxy of string_view as key for associative containers.
Date: Mon, 16 Nov 2020 08:26:32 +0100 [thread overview]
Message-ID: <CAGNvRgD_q0nAW+GbwY264EK3_99T_Toc+ojs37PGC3HYOSOPbA@mail.gmail.com> (raw)
In-Reply-To: <CH2PR02MB6522ACC88083B9E4DD88EDD3B2E30@CH2PR02MB6522.namprd02.prod.outlook.com>
Am Mo., 16. Nov. 2020 um 07:44 Uhr schrieb sotrdg sotrdg via Libstdc++
<libstdc++@gcc.gnu.org>:
>
> The code fails to compile for both unordered_map and map. Is this a bug of libstdc++ or a defect of WG21?
> #include<unordered_map>
> #include<string>
> #include<string_view>
> #include<map>
>
> int main()
> {
> std::unordered_map<std::string,std::string> umap;
> std::string_view key{"a key"};
> auto p{umap.find(key)};
> std::map<std::string,std::string> map;
> auto p1{map.find(key)};
> }
I don't see any WG21 defect involved here, but a mislead user expectation:
Let's start with the observation that this code isn't valid. To
activate transparent comparison, you need to provide function objects
that have the is_transparent tag defined, e.g. aomething like the
following should be more appropriate:
#include<unordered_map>
#include<string>
#include<string_view>
#include<map>
struct hash_transparent
{
using is_transparent = void;
template<class T>
std::size_t operator()(const T& s) const
{
return std::hash<T>()(s);
}
};
int main()
{
std::unordered_map<std::string, std::string, hash_transparent,
std::equal_to<>> umap;
std::string_view key{ "a key" };
auto p{umap.find(key)};
std::map<std::string, std::string, std::less<>> map;
auto p1{map.find(key)};
}
Nonetheless, when you test this, you will still get a compile error
with gcc/clang for the unordered_map (Not for the map), so I'm
assuming that this feature has not been implemented yet. Visual Studio
2019 accepts that code, though.
- Daniel
next prev parent reply other threads:[~2020-11-16 7:26 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-16 6:43 sotrdg sotrdg
2020-11-16 7:26 ` Daniel Krügler [this message]
2020-11-16 9:57 ` Jonathan Wakely
2020-11-28 11:02 ` François Dumont
2020-11-28 11:17 ` Ville Voutilainen
2020-11-28 13:30 ` François Dumont
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAGNvRgD_q0nAW+GbwY264EK3_99T_Toc+ojs37PGC3HYOSOPbA@mail.gmail.com \
--to=daniel.kruegler@gmail.com \
--cc=euloanty@live.com \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).