Some more C++20 changes from P1614R2, "The Mothership has Landed".
This includes the proposed resolution for LWG 3426 to fix the three-way
comparison with nullptr_t.
The existing tests for unique_ptr comparisons don't actually check the
results, only that the expressions compile and are convertible to bool.
This also adds a test for the results of those comparisons for C++11 and
up.
* include/bits/unique_ptr.h (operator<=>): Define for C++20.
* testsuite/20_util/default_delete/48631_neg.cc: Adjust dg-error line.
* testsuite/20_util/default_delete/void_neg.cc: Likewise.
* testsuite/20_util/unique_ptr/comparison/compare.cc: New test.
* testsuite/20_util/unique_ptr/comparison/compare_c++20.cc: New test.
+2020-04-09 Jonathan Wakely <jwakely@redhat.com>
+
+ * include/bits/unique_ptr.h (operator<=>): Define for C++20.
+ * testsuite/20_util/default_delete/48631_neg.cc: Adjust dg-error line.
+ * testsuite/20_util/default_delete/void_neg.cc: Likewise.
+ * testsuite/20_util/unique_ptr/comparison/compare.cc: New test.
+ * testsuite/20_util/unique_ptr/comparison/compare_c++20.cc: New test.
+
2020-04-08 Jonathan Wakely <jwakely@redhat.com>
* include/bits/slice_array.h (operator==(const slice&, const slice&)):
#include <tuple>
#include <bits/stl_function.h>
#include <bits/functional_hash.h>
+#if __cplusplus > 201703L
+# include <compare>
+#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
{ return !__x; }
+#ifndef __cpp_lib_three_way_comparison
/// unique_ptr comparison with nullptr
template<typename _Tp, typename _Dp>
_GLIBCXX_NODISCARD inline bool
_GLIBCXX_NODISCARD inline bool
operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
{ return (bool)__x; }
+#endif // three way comparison
/// Relational operator for unique_ptr objects, compares the owned pointers
template<typename _Tp, typename _Dp,
_GLIBCXX_NODISCARD inline bool
operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
{ return !(nullptr < __x); }
+
+#ifdef __cpp_lib_three_way_comparison
+ template<typename _Tp, typename _Dp, typename _Up, typename _Ep>
+ requires three_way_comparable_with<typename unique_ptr<_Tp, _Dp>::pointer,
+ typename unique_ptr<_Up, _Ep>::pointer>
+ compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
+ typename unique_ptr<_Up, _Ep>::pointer>
+ operator<=>(const unique_ptr<_Tp, _Dp>& __x,
+ const unique_ptr<_Up, _Ep>& __y)
+ { return compare_three_way()(__x.get(), __y.get()); }
+
+ template<typename _Tp, typename _Dp>
+ requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
+ compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer>
+ operator<=>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
+ {
+ using pointer = typename unique_ptr<_Tp, _Dp>::pointer;
+ return compare_three_way()(__x.get(), pointer(nullptr));
+ }
+#endif
// @} relates unique_ptr
/// @cond undocumented
D d;
std::default_delete<B[]> db;
typedef decltype(db(&d)) type; // { dg-error "no match" }
-// { dg-error "no type" "" { target *-*-* } 112 }
+// { dg-error "no type" "" { target *-*-* } 115 }
{
std::default_delete<void> d;
d(nullptr); // { dg-error "here" }
- // { dg-error "incomplete" "" { target *-*-* } 77 }
+ // { dg-error "incomplete" "" { target *-*-* } 80 }
}
--- /dev/null
+// Copyright (C) 2020 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/>.
+
+// { dg-do run { target c++11 } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::unique_ptr<int> p0, p00;
+ VERIFY( p0 == p00 );
+ VERIFY( !(p0 < p00) );
+ VERIFY( !(p0 > p00) );
+ VERIFY( p0 <= p00 );
+ VERIFY( p0 >= p00 );
+
+ std::unique_ptr<int> p1(new int(1));
+ VERIFY( p1 == p1 );
+ VERIFY( !(p1 < p1) );
+ VERIFY( !(p1 > p1) );
+ VERIFY( p1 <= p1 );
+ VERIFY( p1 >= p1 );
+
+ std::unique_ptr<const int> p2(new int(1));
+ VERIFY( p1 >= p1 );
+ VERIFY( p1 != p2 );
+ VERIFY( (p1 < p2) || (p1 > p2) );
+ VERIFY( (p1 <= p2) || (p1 >= p2) );
+
+ VERIFY( p1 != p0 );
+ VERIFY( !(p1 < p0) );
+ VERIFY( p1 > p0 );
+ VERIFY( !(p1 <= p0) );
+ VERIFY( p1 >= p0 );
+}
+
+void
+test02()
+{
+ std::unique_ptr<int> p0;
+ VERIFY( p0 == nullptr );
+ VERIFY( !(p0 < nullptr) );
+ VERIFY( !(p0 > nullptr) );
+ VERIFY( p0 <= nullptr );
+ VERIFY( p0 >= nullptr );
+
+ VERIFY( nullptr == p0 );
+ VERIFY( !(nullptr < p0) );
+ VERIFY( !(nullptr > p0) );
+ VERIFY( nullptr <= p0 );
+ VERIFY( nullptr >= p0 );
+
+ std::unique_ptr<int> p1(new int(1));
+ VERIFY( p1 != nullptr );
+ VERIFY( !(p1 < nullptr) );
+ VERIFY( p1 > nullptr );
+ VERIFY( !(p1 <= nullptr) );
+ VERIFY( p1 >= nullptr );
+
+ VERIFY( nullptr != p1 );
+ VERIFY( nullptr < p1 );
+ VERIFY( !(nullptr > p1) );
+ VERIFY( nullptr <= p1 );
+ VERIFY( !(nullptr >= p1) );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
--- /dev/null
+// Copyright (C) 2020 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+ std::unique_ptr<int> p0, p00;
+ VERIFY( p0 == p00 );
+ VERIFY( !(p0 < p00) );
+ VERIFY( !(p0 > p00) );
+ VERIFY( p0 <= p00 );
+ VERIFY( p0 >= p00 );
+ VERIFY( std::is_eq(p0 <=> p00) );
+
+ std::unique_ptr<int> p1(new int(1));
+ VERIFY( p1 == p1 );
+ VERIFY( !(p1 < p1) );
+ VERIFY( !(p1 > p1) );
+ VERIFY( p1 <= p1 );
+ VERIFY( p1 >= p1 );
+ VERIFY( std::is_eq(p1 <=> p1) );
+
+ std::unique_ptr<const int> p2(new int(1));
+ VERIFY( p1 >= p1 );
+ VERIFY( p1 != p2 );
+ VERIFY( (p1 < p2) || (p1 > p2) );
+ VERIFY( (p1 <= p2) || (p1 >= p2) );
+ VERIFY( std::is_neq(p1 <=> p2) );
+
+ VERIFY( p1 != p0 );
+ VERIFY( !(p1 < p0) );
+ VERIFY( p1 > p0 );
+ VERIFY( !(p1 <= p0) );
+ VERIFY( p1 >= p0 );
+ VERIFY( std::is_gt(p1 <=> p0) );
+ VERIFY( std::is_lt(p0 <=> p1) );
+}
+
+void
+test02()
+{
+ std::unique_ptr<int> p0;
+ VERIFY( p0 == nullptr );
+ VERIFY( !(p0 < nullptr) );
+ VERIFY( !(p0 > nullptr) );
+ VERIFY( p0 <= nullptr );
+ VERIFY( p0 >= nullptr );
+ VERIFY( std::is_eq(p0 <=> nullptr) );
+
+ VERIFY( nullptr == p0 );
+ VERIFY( !(nullptr < p0) );
+ VERIFY( !(nullptr > p0) );
+ VERIFY( nullptr <= p0 );
+ VERIFY( nullptr >= p0 );
+ VERIFY( std::is_eq(nullptr <=> p0) );
+
+ std::unique_ptr<int> p1(new int(1));
+ VERIFY( p1 != nullptr );
+ VERIFY( !(p1 < nullptr) );
+ VERIFY( p1 > nullptr );
+ VERIFY( !(p1 <= nullptr) );
+ VERIFY( p1 >= nullptr );
+ VERIFY( std::is_gt(p1 <=> nullptr) );
+
+ VERIFY( nullptr != p1 );
+ VERIFY( nullptr < p1 );
+ VERIFY( !(nullptr > p1) );
+ VERIFY( nullptr <= p1 );
+ VERIFY( !(nullptr >= p1) );
+ VERIFY( std::is_lt(nullptr <=> p1) );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}