From 8b99f005cbf2fdc6968d75a13cd2c166795418b7 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 15 Nov 2016 14:33:09 +0000 Subject: [PATCH] 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. From-SVN: r242415 --- libstdc++-v3/ChangeLog | 8 ++++ libstdc++-v3/doc/xml/manual/intro.xml | 10 ++++- libstdc++-v3/include/std/optional | 7 ++- .../testsuite/20_util/optional/swap/2.cc | 45 +++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/optional/swap/2.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index d3da57cc9a4..42dedfcee52 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2016-11-15 Jonathan Wakely + + * 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. + 2016-11-14 Ville Voutilainen Implement P0513R0, Poisoning the Hash. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 528b1920cda..0df24bb5f2f 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 ac73ea75bae..ea673cc6e8b 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 00000000000..5793488a375 --- /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> ); -- 2.30.2