From 8b99f005cbf2fdc6968d75a13cd2c166795418b7 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely <jwakely@redhat.com> 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<T>::swap): Use is_nothrow_swappable_v for exception specification. (swap(optional<T>&, optional<T>&)): 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 d3da57cc9a45..42dedfcee526 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2016-11-15 Jonathan Wakely <jwakely@redhat.com> + + * doc/xml/manual/intro.xml: Document LWG 2748 status. + * include/std/optional (optional<T>::swap): Use is_nothrow_swappable_v + for exception specification. + (swap(optional<T>&, optional<T>&)): Disable when T is not swappable. + * testsuite/20_util/optional/swap/2.cc: New test. + 2016-11-14 Ville Voutilainen <ville.voutilainen@gmail.com> Implement P0513R0, Poisoning the Hash. diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 528b1920cdaf..0df24bb5f2f9 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. </para></listitem></varlistentry> <varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../ext/lwg-defects.html#2583">2583</link>: - <emphasis>There is no way to supply an allocator for <code> basic_string(str, pos)</code> + <emphasis>There is no way to supply an allocator for <code>basic_string(str, pos)</code> </emphasis> </term> <listitem><para>Add new constructor @@ -1107,6 +1107,14 @@ requirements of the license of GCC. <listitem><para>Define the <code>value_compare</code> typedef. </para></listitem></varlistentry> + <varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../ext/lwg-defects.html#2748">2748</link>: + <emphasis>swappable traits for optionals + </emphasis> + </term> + <listitem><para>Disable the non-member <code>swap</code> overload when + the contained object is not swappable. + </para></listitem></varlistentry> + </variablelist> </section> diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index ac73ea75bae0..ea673cc6e8b0 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<typename _Tp> - inline void + inline enable_if_t<is_move_constructible_v<_Tp> && 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 000000000000..5793488a3756 --- /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 +// <http://www.gnu.org/licenses/>. + +#include <optional> + +// Swappable. +struct A { }; + +static_assert( std::is_nothrow_swappable_v<A> ); +static_assert( std::is_nothrow_swappable_v<std::optional<A>> ); + +// Swappable, but might throw. +struct B { }; +void swap(B&, B&) noexcept(false); + +static_assert( std::is_swappable_v<std::optional<B>> ); +static_assert( !std::is_nothrow_swappable_v<std::optional<B>> ); + +// Not swappable, but optional<C> is swappable via the generic std::swap. +struct C { }; +void swap(C&, C&) = delete; + +static_assert( std::is_swappable_v<std::optional<C>> ); + +// Not swappable, and optional<D> not swappable via the generic std::swap. +struct D { D(D&&) = delete; }; + +static_assert( !std::is_swappable_v<std::optional<D>> ); -- GitLab