From: Ville Voutilainen Date: Wed, 29 Jul 2015 12:41:23 +0000 (+0300) Subject: re PR libstdc++/60970 (Support std::hash with enum types (LWG 2148)) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1fa9ba224e9c93221cda08900070b7e66b5541ee;p=gcc.git re PR libstdc++/60970 (Support std::hash with enum types (LWG 2148)) 2015-07-29 Ville Voutilainen PR libstdc++/60970 * include/bits/functional_hash.h (__hash_enum): New. (hash): Derive from __hash_enum. * testsuite/20_util/hash/60970.cc: New. From-SVN: r226347 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 4447b3d9f48..f0f8c1aed83 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2015-07-29 Ville Voutilainen + + PR libstdc++/60970 + * include/bits/functional_hash.h (__hash_enum): New. + (hash): Derive from __hash_enum. + * testsuite/20_util/hash/60970.cc: New. + 2015-07-29 Tim Shen PR libstdc++/67015 diff --git a/libstdc++-v3/include/bits/functional_hash.h b/libstdc++-v3/include/bits/functional_hash.h index 3c962fc7a91..88937bdc4af 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 struct hash; + // Helper struct for SFINAE-poisoning non-enum types. + template::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 + struct __hash_enum<_Tp, true> : public __hash_base + { + 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 + struct hash : __hash_enum<_Tp> + { }; + /// Partial specializations for pointer types. template struct hash<_Tp*> : public __hash_base 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 00000000000..ddc626f81cb --- /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 +// . + + +#include +#include + +using namespace std; + +enum E1 : int {FIRST=1, SECOND=2}; +enum class E2 : int {THIRD=42, FOURTH=666}; + +int main() +{ + VERIFY(hash{}(1) == hash{}(FIRST)); + VERIFY(hash{}(2) == hash{}(SECOND)); + VERIFY(hash{}(42) == hash{}(E2::THIRD)); + VERIFY(hash{}(666) == hash{}(E2::FOURTH)); +}