From patchwork Wed Jul 29 12:42:35 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 501700 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 541F21402B8 for ; Wed, 29 Jul 2015 22:43:00 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=Lnu3UrZh; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=hoN2vNRW8XXzscYkb f1K0O69f6ho6//Kqm9eeftRAlL3oK09Cj+HZZFYcOQezWhoV8eU2fNDRp5B8ZuBd TYCxL9wr4WValXpe1T27+XEZayZSUGYvcNHJ/JsqnmDt/x0R0OeUG4EDzNmuxMgQ ESO/pQcmKTgo8yjBDJ+LIp86g4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=default; bh=KEmf1Y/G3RN1sB86cQLNGOk i3+I=; b=Lnu3UrZhh4+SQnBKNhkbxbKdviPHLl4cbL2pPDsGcrMN0QpBxtM47wx Aj9yawFwrQFbSf00/nj7+GtBhWtQ9gB3T7U+LUi9IqMkx1sIdKBePSJque+wHSJp WYct201g3bsZS1eW2hsj6JzWiw6V9Ws6WDhL8qqGQ28gpq574FMc= Received: (qmail 23351 invoked by alias); 29 Jul 2015 12:42:45 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 23315 invoked by uid 89); 29 Jul 2015 12:42:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.6 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, POKER_BODY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=no version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 29 Jul 2015 12:42:38 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 2DBD3A2056; Wed, 29 Jul 2015 12:42:37 +0000 (UTC) Received: from localhost (ovpn-116-56.ams2.redhat.com [10.36.116.56]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t6TCgah4026423; Wed, 29 Jul 2015 08:42:36 -0400 Date: Wed, 29 Jul 2015 13:42:35 +0100 From: Jonathan Wakely To: Ville Voutilainen Cc: libstdc++@gcc.gnu.org, "gcc-patches@gcc.gnu.org" Subject: Re: [v3 PATCH] PR libstdc++/60970, implement LWG 2148 Message-ID: <20150729124235.GF13355@redhat.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.5.23 (2014-03-12) 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. commit 7d23fa744a6eb0ee4d0df9f044b72cb172336915 Author: Jonathan Wakely Date: Wed Jul 29 11:09:52 2015 +0100 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. 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 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 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 +// . + + +#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)); +}