* [v3 PATCH] PR libstdc++/60970, implement LWG 2148
@ 2015-07-25 19:40 Ville Voutilainen
2015-07-29 13:25 ` Jonathan Wakely
0 siblings, 1 reply; 2+ messages in thread
From: Ville Voutilainen @ 2015-07-25 19:40 UTC (permalink / raw)
To: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 940 bytes --]
Tested on Linux-PPC64.
The proposed resolution of the issue doesn't really say whether our
regression test for PR libstdc++/52931 should remain valid. However,
it doesn't say that we shouldn't keep it valid, either. This approach
keeps it valid, but provides support for hashing enums. It took a while
to figure out suitable jiggery-pokery to make it so, but this approach
passes the testsuite without regressions. I considered an alternative
alias-template-based approach, but while that attempt would've worked
with our current front-end, it would not have worked on clang (and
it's thus far unclear whether it was intended to work by the language
rules).
2015-07-25 Ville Voutilainen <ville.voutilainen@gmail.com>
PR libstdc++/60970, implement LWG 2148, hash support for
enum types.
* include/bits/functional_hash.h
(__hash_enum): New.
(hash): Derive from __hash_enum.
* testsuite/20_util/hash/60970.cc: New.
[-- Attachment #2: enum-hash.diff --]
[-- Type: text/plain, Size: 2676 bytes --]
diff --git a/libstdc++-v3/include/bits/functional_hash.h b/libstdc++-v3/include/bits/functional_hash.h
index d94843f..f81864f 100644
--- a/libstdc++-v3/include/bits/functional_hash.h
+++ b/libstdc++-v3/include/bits/functional_hash.h
@@ -57,6 +57,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct hash;
+ // Helper struct for SFINAE-poisoning non-enum types.
+ template<typename _Tp, bool>
+ struct __hash_enum
+ {
+ private:
+ __hash_enum(__hash_enum&&);
+ ~__hash_enum();
+ };
+
+ // Helper struct for hash with enum types.
+ template<typename _Tp>
+ struct __hash_enum<_Tp, true> : public __hash_base<size_t, _Tp>
+ {
+ size_t
+ operator()(_Tp __val) const noexcept
+ {
+ using __type = typename underlying_type<_Tp>::type;
+ return hash<__type>{}(static_cast<__type>(__val));
+ }
+ };
+
+ /// Primary class template hash, usable for enum types only.
+ // Use with non-enum types still SFINAES.
+ template<typename _Tp>
+ struct hash : __hash_enum<_Tp, is_enum<_Tp>::value>
+ { };
+
/// Partial specializations for pointer types.
template<typename _Tp>
struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
diff --git a/libstdc++-v3/testsuite/20_util/hash/60970.cc b/libstdc++-v3/testsuite/20_util/hash/60970.cc
new file mode 100644
index 0000000..ddc626f
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/hash/60970.cc
@@ -0,0 +1,36 @@
+// { dg-options "-std=gnu++11" }
+// { dg-do run }
+
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+
+#include <functional>
+#include <testsuite_hooks.h>
+
+using namespace std;
+
+enum E1 : int {FIRST=1, SECOND=2};
+enum class E2 : int {THIRD=42, FOURTH=666};
+
+int main()
+{
+ VERIFY(hash<int>{}(1) == hash<E1>{}(FIRST));
+ VERIFY(hash<int>{}(2) == hash<E1>{}(SECOND));
+ VERIFY(hash<int>{}(42) == hash<E2>{}(E2::THIRD));
+ VERIFY(hash<int>{}(666) == hash<E2>{}(E2::FOURTH));
+}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [v3 PATCH] PR libstdc++/60970, implement LWG 2148
2015-07-25 19:40 [v3 PATCH] PR libstdc++/60970, implement LWG 2148 Ville Voutilainen
@ 2015-07-29 13:25 ` Jonathan Wakely
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2015-07-29 13:25 UTC (permalink / raw)
To: Ville Voutilainen; +Cc: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 844 bytes --]
On 25/07/15 22:01 +0300, Ville Voutilainen wrote:
>Tested on Linux-PPC64.
>
>The proposed resolution of the issue doesn't really say whether our
>regression test for PR libstdc++/52931 should remain valid. However,
>it doesn't say that we shouldn't keep it valid, either. This approach
>keeps it valid, but provides support for hashing enums. It took a while
>to figure out suitable jiggery-pokery to make it so, but this approach
>passes the testsuite without regressions. I considered an alternative
>alias-template-based approach, but while that attempt would've worked
>with our current front-end, it would not have worked on clang (and
>it's thus far unclear whether it was intended to work by the language
>rules).
Committed with a minor change to put is_enum<_Tp>::value as a default
template argument of __hash_enum and add a comment.
[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 3114 bytes --]
commit 7d23fa744a6eb0ee4d0df9f044b72cb172336915
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Wed Jul 29 11:09:52 2015 +0100
2015-07-29 Ville Voutilainen <ville.voutilainen@gmail.com>
PR libstdc++/60970
* include/bits/functional_hash.h (__hash_enum): New.
(hash): Derive from __hash_enum.
* testsuite/20_util/hash/60970.cc: New.
diff --git a/libstdc++-v3/include/bits/functional_hash.h b/libstdc++-v3/include/bits/functional_hash.h
index 3c962fc..88937bd 100644
--- a/libstdc++-v3/include/bits/functional_hash.h
+++ b/libstdc++-v3/include/bits/functional_hash.h
@@ -57,6 +57,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp>
struct hash;
+ // Helper struct for SFINAE-poisoning non-enum types.
+ template<typename _Tp, bool = is_enum<_Tp>::value>
+ struct __hash_enum
+ {
+ private:
+ // Private rather than deleted to be non-trivially-copyable.
+ __hash_enum(__hash_enum&&);
+ ~__hash_enum();
+ };
+
+ // Helper struct for hash with enum types.
+ template<typename _Tp>
+ struct __hash_enum<_Tp, true> : public __hash_base<size_t, _Tp>
+ {
+ size_t
+ operator()(_Tp __val) const noexcept
+ {
+ using __type = typename underlying_type<_Tp>::type;
+ return hash<__type>{}(static_cast<__type>(__val));
+ }
+ };
+
+ /// Primary class template hash, usable for enum types only.
+ // Use with non-enum types still SFINAES.
+ template<typename _Tp>
+ struct hash : __hash_enum<_Tp>
+ { };
+
/// Partial specializations for pointer types.
template<typename _Tp>
struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
diff --git a/libstdc++-v3/testsuite/20_util/hash/60970.cc b/libstdc++-v3/testsuite/20_util/hash/60970.cc
new file mode 100644
index 0000000..ddc626f
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/hash/60970.cc
@@ -0,0 +1,36 @@
+// { dg-options "-std=gnu++11" }
+// { dg-do run }
+
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+
+#include <functional>
+#include <testsuite_hooks.h>
+
+using namespace std;
+
+enum E1 : int {FIRST=1, SECOND=2};
+enum class E2 : int {THIRD=42, FOURTH=666};
+
+int main()
+{
+ VERIFY(hash<int>{}(1) == hash<E1>{}(FIRST));
+ VERIFY(hash<int>{}(2) == hash<E1>{}(SECOND));
+ VERIFY(hash<int>{}(42) == hash<E2>{}(E2::THIRD));
+ VERIFY(hash<int>{}(666) == hash<E2>{}(E2::FOURTH));
+}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-07-29 12:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-25 19:40 [v3 PATCH] PR libstdc++/60970, implement LWG 2148 Ville Voutilainen
2015-07-29 13:25 ` Jonathan Wakely
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).