From 4ef5bbd81536351d78bae8dc22c8ebdc6e6c6500 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Fri, 6 Sep 2019 13:54:51 +0100 Subject: [PATCH] Define std::ssize for C++20 (P1227R2) * include/bits/range_access.h (ssize): Define for C++20. * testsuite/24_iterators/range_access_cpp20.cc: New test. * doc/xml/manual/status_cxx2020.xml: Update P1227R2 status. * doc/html/*: Regenerate. From-SVN: r275458 --- libstdc++-v3/ChangeLog | 7 +++ libstdc++-v3/doc/html/manual/status.html | 4 +- libstdc++-v3/doc/xml/manual/status_cxx2020.xml | 3 +- libstdc++-v3/include/bits/range_access.h | 15 +++++ .../testsuite/24_iterators/range_access_cpp20.cc | 67 ++++++++++++++++++++++ 5 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 libstdc++-v3/testsuite/24_iterators/range_access_cpp20.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ad96489..e135e85 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2019-09-06 Jonathan Wakely + + * include/bits/range_access.h (ssize): Define for C++20. + * testsuite/24_iterators/range_access_cpp20.cc: New test. + * doc/xml/manual/status_cxx2020.xml: Update P1227R2 status. + * doc/html/*: Regenerate. + 2019-09-06 Florian Weimer * configure: Regenerate. diff --git a/libstdc++-v3/doc/html/manual/status.html b/libstdc++-v3/doc/html/manual/status.html index 06269a7..4442e19 100644 --- a/libstdc++-v3/doc/html/manual/status.html +++ b/libstdc++-v3/doc/html/manual/status.html @@ -1422,11 +1422,11 @@ Feature-testing recommendations for C++. P1001R2 -   Signed ssize() functions, unsigned size() functions +   Signed ssize() functions, unsigned size() functions P1227R2 -   Ranges Design Cleanup + 10.1   Ranges Design Cleanup P1252R2 diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2020.xml b/libstdc++-v3/doc/xml/manual/status_cxx2020.xml index ffd73ae..fa0c89d 100644 --- a/libstdc++-v3/doc/xml/manual/status_cxx2020.xml +++ b/libstdc++-v3/doc/xml/manual/status_cxx2020.xml @@ -1086,14 +1086,13 @@ Feature-testing recommendations for C++. - Signed ssize() functions, unsigned size() functions P1227R2 - + 10.1 diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h index 44b9c6c..c574414 100644 --- a/libstdc++-v3/include/bits/range_access.h +++ b/libstdc++-v3/include/bits/range_access.h @@ -319,6 +319,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif // C++17 #if __cplusplus > 201703L + template + constexpr auto + ssize(const _Container& __cont) + noexcept(noexcept(__cont.size())) + -> common_type_t> + { + using type = make_signed_t; + return static_cast>(__cont.size()); + } + + template + constexpr ptrdiff_t + ssize(const _Tp (&)[_Num]) noexcept + { return _Num; } + // "why are these in namespace std:: and not __gnu_cxx:: ?" // because if we don't put them here it's impossible to // have implicit ADL with "using std::begin/end/size/data;". diff --git a/libstdc++-v3/testsuite/24_iterators/range_access_cpp20.cc b/libstdc++-v3/testsuite/24_iterators/range_access_cpp20.cc new file mode 100644 index 0000000..567b056 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_access_cpp20.cc @@ -0,0 +1,67 @@ +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +// Copyright (C) 2019 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 +// . + +// N4830 23.7, Range access [iterator.range] + +#include + +void +test01() +{ + static int i[1]; + constexpr auto s = std::ssize(i); + const std::ptrdiff_t* check_type = &s; + static_assert(s == 1); +} + +void +test02() +{ + static int i[] = { 1, 2 }; + constexpr auto s = std::ssize(i); + const std::ptrdiff_t* check_type = &s; + static_assert(s == 2); +} + +void +test03() +{ + struct Cont + { + constexpr unsigned short size() const { return 3; } + }; + constexpr Cont c; + constexpr auto s = std::ssize(c); + const std::ptrdiff_t* check_type = &s; + static_assert(s == 3); +} + +void +test04() +{ + struct Cont + { + constexpr unsigned long long size() const { return 4; } + }; + constexpr Cont c; + constexpr auto s = std::ssize(c); + const long long* check_type = &s; + static_assert(s == 4); +} -- 2.7.4