From 7ab36231a17d8a78f4355289ebbd9d32bb8ede7b Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 17 Feb 2020 17:58:09 +0000 Subject: [PATCH] libstdc++: P1970R2 Consistency for size() functions: Add ranges::ssize This defines ranges::ssize as approved in Prague. It's unclear what is supposed to happen for types for which range_difference_t is not a valid type. I've assumed they are not meant to be usable with ranges::ssize, despite being usable with ranges::size. * include/bits/range_access.h (_SSize, ssize): Define for C++20. * testsuite/std/ranges/access/ssize.cc: New test. --- libstdc++-v3/ChangeLog | 4 + libstdc++-v3/include/bits/range_access.h | 28 ++++++ .../testsuite/std/ranges/access/ssize.cc | 98 +++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 libstdc++-v3/testsuite/std/ranges/access/ssize.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 999c35fb9bc..547337dda90 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,9 @@ 2020-02-17 Jonathan Wakely + P1970R2 Consistency for size() functions: Add ranges::ssize + * include/bits/range_access.h (_SSize, ssize): Define for C++20. + * testsuite/std/ranges/access/ssize.cc: New test. + P1956R1 On the names of low-level bit manipulation functions * include/bits/hashtable_policy.h: Update comment. * include/std/bit (__ispow2, __ceil2, __floor2, __log2p1): Rename. diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h index 8b546a58840..8bac0efc6ed 100644 --- a/libstdc++-v3/include/bits/range_access.h +++ b/libstdc++-v3/include/bits/range_access.h @@ -35,6 +35,7 @@ #if __cplusplus >= 201103L #include #include +#include namespace std _GLIBCXX_VISIBILITY(default) { @@ -723,6 +724,32 @@ namespace ranges } }; + struct _SSize + { + template + requires requires (_Tp&& __e) + { + _Begin{}(std::forward<_Tp>(__e)); + _Size{}(std::forward<_Tp>(__e)); + } + constexpr auto + operator()(_Tp&& __e) const + noexcept(noexcept(_Size{}(std::forward<_Tp>(__e)))) + { + using __iter_type = decltype(_Begin{}(std::forward<_Tp>(__e))); + using __diff_type = iter_difference_t<__iter_type>; + using std::__detail::__int_limits; + auto __size = _Size{}(std::forward<_Tp>(__e)); + if constexpr (integral<__diff_type>) + { + if constexpr (__int_limits<__diff_type>::digits + < __int_limits::digits) + return static_cast(__size); + } + return static_cast<__diff_type>(__size); + } + }; + template concept __member_empty = requires(_Tp&& __t) { bool(std::forward<_Tp>(__t).empty()); }; @@ -834,6 +861,7 @@ namespace ranges inline constexpr __cust_access::_CRBegin crbegin{}; inline constexpr __cust_access::_CREnd crend{}; inline constexpr __cust_access::_Size size{}; + inline constexpr __cust_access::_SSize ssize{}; inline constexpr __cust_access::_Empty empty{}; inline constexpr __cust_access::_Data data{}; inline constexpr __cust_access::_CData cdata{}; diff --git a/libstdc++-v3/testsuite/std/ranges/access/ssize.cc b/libstdc++-v3/testsuite/std/ranges/access/ssize.cc new file mode 100644 index 00000000000..5aa05be8f20 --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/access/ssize.cc @@ -0,0 +1,98 @@ +// 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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +using std::ptrdiff_t; + +void +test01() +{ + constexpr int a[10] = { }; + static_assert( std::same_as ); + static_assert( std::ranges::ssize(a) == 10 ); + static_assert( noexcept(std::ranges::ssize(a)) ); + + int a2[2]; + static_assert( std::same_as ); + VERIFY( std::ranges::ssize(a2) == 2); + static_assert( noexcept(std::ranges::ssize(a2)) ); + + struct Incomplete; + using A = Incomplete[2]; // bounded array of incomplete type + extern A& f(); + static_assert( std::same_as ); +} + +void +test02() +{ + int a[3] = { }; + __gnu_test::test_sized_range ri(a); + VERIFY( std::ranges::ssize(ri) == 3 ); + static_assert( noexcept(std::ranges::ssize(ri)) ); +} + +void +test04() +{ + int a[] = { 0, 1 }; + __gnu_test::test_range r(a); + VERIFY( std::ranges::ssize(r) == std::ranges::end(r) - std::ranges::begin(r) ); +} + +struct R5 +{ + int size() const noexcept { return 0; } + R5* begin() { return this; } + R5* end() { return this + 1; } +}; + +template<> +constexpr bool std::ranges::disable_sized_range = true; + +void +test05() +{ + R5 r; + VERIFY( std::ranges::ssize(r) == 1 ); +} + +void +test06() +{ + auto i = std::views::iota(1ull, 5); + auto s = std::ranges::ssize(i); + using R = std::ranges::range_difference_t; + static_assert( std::same_as ); + VERIFY( s == 4 ); +} + +int +main() +{ + test01(); + test02(); + test04(); + test05(); + test06(); +} -- 2.30.2