]> gcc.gnu.org Git - gcc.git/commitdiff
Constrain swap overload for std::optional (LWG 2748)
authorJonathan Wakely <jwakely@redhat.com>
Tue, 15 Nov 2016 14:33:09 +0000 (14:33 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 15 Nov 2016 14:33:09 +0000 (14:33 +0000)
* 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
libstdc++-v3/doc/xml/manual/intro.xml
libstdc++-v3/include/std/optional
libstdc++-v3/testsuite/20_util/optional/swap/2.cc [new file with mode: 0644]

index d3da57cc9a45b8b637be599d2c57f02f70d9c596..42dedfcee526a1b1c8f69d55317e0df717f744d1 100644 (file)
@@ -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.
index 528b1920cdafb8eb85a206857bbadd6e1e71455c..0df24bb5f2f93809f7864c17935363c4e01eda76 100644 (file)
@@ -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>
index ac73ea75bae0099aaa1c1120a6237ad39732efa9..ea673cc6e8b0b830ce57b198c7caf63e3f4bba25 100644 (file)
@@ -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 (file)
index 0000000..5793488
--- /dev/null
@@ -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>> );
This page took 0.082902 seconds and 5 git commands to generate.