This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Constrain swap overload for std::optional (LWG 2748)


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<NonSwappable> 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<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.

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 <jwakely@redhat.com>
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<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.

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.
     </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 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<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 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
+// <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>> );

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]