2020-04-14 Jonathan Wakely <jwakely@redhat.com>
+ PR libstdc++/94562
+ * include/bits/shared_ptr.h (operator<=>): Define for C++20.
+ * include/bits/shared_ptr_base.h (operator<=>): Likewise.
+ * include/bits/unique_ptr.h (operator<=>): Add inline specifier.
+ * testsuite/20_util/shared_ptr/comparison/cmp_c++20.cc: New test.
+ * testsuite/20_util/shared_ptr/comparison/less.cc: Do not expect
+ std::less<A*> to be used when comparing std::shared_ptr<A> objects in
+ C++20.
+
PR libstdc++/94565
* libsupc++/compare (__unspec): Add noexcept-specifier to constructor.
* testsuite/18_support/comparisons/categories/94565.cc: New test.
operator==(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
{ return !__a; }
+#ifdef __cpp_lib_three_way_comparison
+ template<typename _Tp, typename _Up>
+ inline strong_ordering
+ operator<=>(const shared_ptr<_Tp>& __a,
+ const shared_ptr<_Up>& __b) noexcept
+ { return compare_three_way()(__a.get(), __b.get()); }
+
+ template<typename _Tp>
+ inline strong_ordering
+ operator<=>(const shared_ptr<_Tp>& __a, nullptr_t) noexcept
+ {
+ using pointer = typename shared_ptr<_Tp>::element_type*;
+ return compare_three_way()(__a.get(), static_cast<pointer>(nullptr));
+ }
+#else
/// shared_ptr comparison with nullptr
template<typename _Tp>
_GLIBCXX_NODISCARD inline bool
_GLIBCXX_NODISCARD inline bool
operator>=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
{ return !(nullptr < __a); }
+#endif
// 20.7.2.2.8 shared_ptr specialized algorithms.
#include <bits/refwrap.h>
#include <bits/stl_function.h>
#include <ext/aligned_buffer.h>
+#if __cplusplus > 201703L
+# include <compare>
+#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
operator==(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
{ return !__a; }
+#ifdef __cpp_lib_three_way_comparison
+ template<typename _Tp, typename _Up, _Lock_policy _Lp>
+ inline strong_ordering
+ operator<=>(const __shared_ptr<_Tp, _Lp>& __a,
+ const __shared_ptr<_Up, _Lp>& __b) noexcept
+ { return compare_three_way()(__a.get(), __b.get()); }
+
+ template<typename _Tp, _Lock_policy _Lp>
+ inline strong_ordering
+ operator<=>(const __shared_ptr<_Tp, _Lp>& __a, nullptr_t) noexcept
+ {
+ using pointer = typename __shared_ptr<_Tp, _Lp>::element_type*;
+ return compare_three_way()(__a.get(), static_cast<pointer>(nullptr));
+ }
+#else
template<typename _Tp, _Lock_policy _Lp>
inline bool
operator==(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
inline bool
operator>=(nullptr_t, const __shared_ptr<_Tp, _Lp>& __a) noexcept
{ return !(nullptr < __a); }
+#endif // three-way comparison
// 20.7.2.2.8 shared_ptr specialized algorithms.
template<typename _Tp, _Lock_policy _Lp>
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>
+ inline
compare_three_way_result_t<typename unique_ptr<_Tp, _Dp>::pointer,
typename unique_ptr<_Up, _Ep>::pointer>
operator<=>(const unique_ptr<_Tp, _Dp>& __x,
template<typename _Tp, typename _Dp>
requires three_way_comparable<typename unique_ptr<_Tp, _Dp>::pointer>
+ inline
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));
+ return compare_three_way()(__x.get(), static_cast<pointer>(nullptr));
}
#endif
// @} relates unique_ptr
--- /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::shared_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::shared_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::shared_ptr<int> p11 = p1;
+ VERIFY( p11 == p1 );
+ VERIFY( !(p11 < p1) );
+ VERIFY( !(p11 > p1) );
+ VERIFY( p11 <= p1 );
+ VERIFY( p11 >= p1 );
+ VERIFY( std::is_eq(p11 <=> p1) );
+
+ std::shared_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::shared_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::shared_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();
+}
std::shared_ptr<A> p1;
std::shared_ptr<A> p2;
VERIFY( !less(p1, p2) && !less(p2, p1) );
+#ifndef __cpp_lib_three_way_comparison
+// In C++20 std::less<std::shared_ptr<A>> uses the operator< synthesized
+// from operator<=>, which uses std::compare_three_way not std::less<A*>.
VERIFY( std::less<A*>::count == 2 );
+#endif
return 0;
}
return 0;
}
-int
+int
main()
{
test01();