From patchwork Tue Nov 15 14:33:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 82353 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp1559078qge; Tue, 15 Nov 2016 06:33:51 -0800 (PST) X-Received: by 10.99.43.8 with SMTP id r8mr78837696pgr.83.1479220431479; Tue, 15 Nov 2016 06:33:51 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id u20si26716304pfd.147.2016.11.15.06.33.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Nov 2016 06:33:51 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-441456-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-441456-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-441456-patch=linaro.org@gcc.gnu.org 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:subject:message-id:mime-version:content-type; q=dns; s= default; b=WxX//IkU4lcIIklSiCgRjFrOdnL7Ln17s53xXGtYW7inHanwPbK5t R9nR+1do2D2NdhZ22SX5SYPXnKPmByGTTym68iNSuzg1SZG85p1Eit0dOJiYbdj/ v9//bqlzjrPcbtrYw2jNLs2CRxWENMHJ5cUrnwzmWUMYvDQy/buCVU= 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:subject:message-id:mime-version:content-type; s= default; bh=68uWeSOmo9FIhcE1f4IDNuVqjFo=; b=U1RqK2TytAYW30H1Ukru QAFCme4oh30n9gMYTmSmf6xlYrNp3Zdhm4umvZtmRB61X2z5ftzi7aDBvLpm+D0y NIUteQZz4McIVpoy2LxNF2/N3ySOy9QHprI4XtqYYozPm7Kvq6fSYBT2PQJAdhGo /c2eIkKhXVn6lBwXJ1L173E= Received: (qmail 77105 invoked by alias); 15 Nov 2016 14:33:30 -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 77087 invoked by uid 89); 15 Nov 2016 14:33:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.7 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=declval, sk:is_noth, Constrain, UD:xml 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 ESMTP; Tue, 15 Nov 2016 14:33:18 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4B3FBC04D2B1; Tue, 15 Nov 2016 14:33:17 +0000 (UTC) Received: from localhost (ovpn-116-110.ams2.redhat.com [10.36.116.110]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id uAFEXGXV006796; Tue, 15 Nov 2016 09:33:16 -0500 Date: Tue, 15 Nov 2016 14:33:16 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Constrain swap overload for std::optional (LWG 2748) Message-ID: <20161115143315.GF3145@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.7.1 (2016-10-04) This implements the resolution to LWG 2748 which was approved the other day at the WG21 meeting. I think the resolution is wrong, because as the test shows it means that optional is swappable in some cases. I've raised that with the LWG and will probably create a new issue for it, but in the meantime this implements what the spec says. * doc/xml/manual/intro.xml: Document LWG 2748 status. * include/std/optional (optional::swap): Use is_nothrow_swappable_v for exception specification. (swap(optional&, optional&)): Disable when T is not swappable. * testsuite/20_util/optional/swap/2.cc: New test. Tested powerpc64le-linux. This only affects experimental C++17 stuff, so I'm committing it even though we're in Stage 3. commit 9f7a9a3b0091dd09870e4044cacdb4db95a994ab Author: Jonathan Wakely Date: Tue Nov 15 12:06:08 2016 +0000 Constrain swap overload for std::optional (LWG 2748) * doc/xml/manual/intro.xml: Document LWG 2748 status. * include/std/optional (optional::swap): Use is_nothrow_swappable_v for exception specification. (swap(optional&, optional&)): Disable when T is not swappable. * testsuite/20_util/optional/swap/2.cc: New test. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 528b192..0df24bb 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -1094,7 +1094,7 @@ requirements of the license of GCC. 2583: - There is no way to supply an allocator for basic_string(str, pos) + There is no way to supply an allocator for basic_string(str, pos) Add new constructor @@ -1107,6 +1107,14 @@ requirements of the license of GCC. Define the value_compare typedef. + 2748: + swappable traits for optionals + + + Disable the non-member swap overload when + the contained object is not swappable. + + diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index ac73ea7..ea673cc 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -613,7 +613,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void swap(optional& __other) noexcept(is_nothrow_move_constructible<_Tp>() - && noexcept(swap(declval<_Tp&>(), declval<_Tp&>()))) + && is_nothrow_swappable_v<_Tp>) { using std::swap; @@ -920,8 +920,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return !__rhs || __lhs >= *__rhs; } // Swap and creation functions. + + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2748. swappable traits for optionals template - inline void + inline enable_if_t && is_swappable_v<_Tp>> swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs) noexcept(noexcept(__lhs.swap(__rhs))) { __lhs.swap(__rhs); } diff --git a/libstdc++-v3/testsuite/20_util/optional/swap/2.cc b/libstdc++-v3/testsuite/20_util/optional/swap/2.cc new file mode 100644 index 0000000..5793488 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/swap/2.cc @@ -0,0 +1,45 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile } + +// Copyright (C) 2016 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 + +// Swappable. +struct A { }; + +static_assert( std::is_nothrow_swappable_v ); +static_assert( std::is_nothrow_swappable_v> ); + +// Swappable, but might throw. +struct B { }; +void swap(B&, B&) noexcept(false); + +static_assert( std::is_swappable_v> ); +static_assert( !std::is_nothrow_swappable_v> ); + +// Not swappable, but optional is swappable via the generic std::swap. +struct C { }; +void swap(C&, C&) = delete; + +static_assert( std::is_swappable_v> ); + +// Not swappable, and optional not swappable via the generic std::swap. +struct D { D(D&&) = delete; }; + +static_assert( !std::is_swappable_v> );