From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1984) id 1C8903A19009; Fri, 17 Jul 2020 14:30:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1C8903A19009 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1594996217; bh=lsRkMTDR8VXHxVf9gfwkQ6FowEobcOjA1ty56ozNqBw=; h=From:To:Subject:Date:From; b=eOiNU27gX64QxRZaFSCk3XVRdr/AiXCusdNxm2NO2pj0q4unpPYHpEHA6DUv0+MOn XmRl0WWBK86HRhRhvFJVkg1aYODZe3dO77iQ/g4CAOGpw+6Vua+rbuphZLQhIIWJok iFH7j1nDdgTxyDiSxS+Bbs3/QqrUrmEBTTK0Alro= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Tamar Christina To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/vendors/ARM/heads/arm-struct-reorg-wip)] libstdc++: Add comparison operators to types X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/vendors/ARM/heads/arm-struct-reorg-wip X-Git-Oldrev: c9960294062dbda0847d26a1b5ee37a55210d69c X-Git-Newrev: 27c171775abb943d59e2b3fb6fbc0b3680fc7a39 Message-Id: <20200717143017.1C8903A19009@sourceware.org> Date: Fri, 17 Jul 2020 14:30:17 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 17 Jul 2020 14:30:17 -0000 https://gcc.gnu.org/g:27c171775abb943d59e2b3fb6fbc0b3680fc7a39 commit 27c171775abb943d59e2b3fb6fbc0b3680fc7a39 Author: Jonathan Wakely Date: Sat Apr 18 00:47:45 2020 +0100 libstdc++: Add comparison operators to types Some more C++20 changes from P1614R2, "The Mothership has Landed". * include/std/chrono (duration, time_point): Define operator<=> and remove redundant operator!= for C++20. * testsuite/20_util/duration/comparison_operators/three_way.cc: New test. * testsuite/20_util/time_point/comparison_operators/three_way.cc: New test. Diff: --- libstdc++-v3/ChangeLog | 7 +++ libstdc++-v3/include/std/chrono | 24 +++++++++ .../duration/comparison_operators/three_way.cc | 62 ++++++++++++++++++++++ .../time_point/comparison_operators/three_way.cc | 41 ++++++++++++++ 4 files changed, 134 insertions(+) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 64c862c1d03..40345cc7044 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,12 @@ 2020-04-18 Jonathan Wakely + * include/std/chrono (duration, time_point): Define operator<=> and + remove redundant operator!= for C++20. + * testsuite/20_util/duration/comparison_operators/three_way.cc: New + test. + * testsuite/20_util/time_point/comparison_operators/three_way.cc: New + test. + * testsuite/util/native_type/native_priority_queue.hpp: Use allocator_traits to rebind allocator. diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index 514926c5c05..6d78f32ac78 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -43,6 +43,7 @@ #include // for literals support. #if __cplusplus > 201703L # include +# include #endif namespace std _GLIBCXX_VISIBILITY(default) @@ -668,12 +669,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __ct(__lhs).count() < __ct(__rhs).count(); } +#if __cpp_lib_three_way_comparison + template + requires three_way_comparable> + constexpr auto + operator<=>(const duration<_Rep1, _Period1>& __lhs, + const duration<_Rep2, _Period2>& __rhs) + { + using __ct = common_type_t, + duration<_Rep2, _Period2>>; + return __ct(__lhs).count() <=> __ct(__rhs).count(); + } +#else template constexpr bool operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) { return !(__lhs == __rhs); } +#endif template @@ -903,11 +918,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const time_point<_Clock, _Dur2>& __rhs) { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); } +#if __cpp_lib_three_way_comparison + template _Dur2> + constexpr auto + operator<=>(const time_point<_Clock, _Dur1>& __lhs, + const time_point<_Clock, _Dur2>& __rhs) + { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); } +#else template constexpr bool operator!=(const time_point<_Clock, _Dur1>& __lhs, const time_point<_Clock, _Dur2>& __rhs) { return !(__lhs == __rhs); } +#endif template constexpr bool diff --git a/libstdc++-v3/testsuite/20_util/duration/comparison_operators/three_way.cc b/libstdc++-v3/testsuite/20_util/duration/comparison_operators/three_way.cc new file mode 100644 index 00000000000..12c20f82811 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/duration/comparison_operators/three_way.cc @@ -0,0 +1,62 @@ +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +// 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 +// . + +// 20.8.3 Class template duration [time.duration] + +#include +#include + +// C++20 27.5.6 Comparisons [time.duration.comparisons] + +void +test01() +{ + using namespace std::chrono; + + duration d0(12); + duration d1(3); + duration d2(3); + + VERIFY(d1 < d0); + VERIFY(d0 > d1); + VERIFY( std::is_lt(d1 <=> d0) ); + VERIFY( std::is_gt(d0 <=> d1) ); + + VERIFY(d0 != d1); + VERIFY(d1 == d2); + VERIFY( std::is_neq(d0 <=> d1) ); + VERIFY( std::is_eq(d1 <=> d2) ); + + VERIFY(d1 <= d2); + VERIFY(d1 >= d2); + VERIFY( std::is_lteq(d1 <=> d2) ); + VERIFY( std::is_gteq(d1 <=> d2) ); + + VERIFY(d1 <= d0); + VERIFY(d0 >= d1); + VERIFY( std::is_lteq(d1 <=> d0) ); + VERIFY( std::is_gteq(d0 <=> d1) ); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/time_point/comparison_operators/three_way.cc b/libstdc++-v3/testsuite/20_util/time_point/comparison_operators/three_way.cc new file mode 100644 index 00000000000..b753bcf253e --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/time_point/comparison_operators/three_way.cc @@ -0,0 +1,41 @@ +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +// 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 +// . + +#include +#include + +// C++20 27.6.6 Comparisons [time.point.comparisons] + +void +test01() +{ + using namespace std::chrono; + + auto ns = system_clock::now(); + auto s = time_point_cast(ns + seconds(2)); + + VERIFY( s != ns ); + VERIFY( std::is_lt(ns <=> s) ); +} + +int main() +{ + test01(); +}