Define std::ssize for C++20 (P1227R2)
authorJonathan Wakely <jwakely@redhat.com>
Fri, 6 Sep 2019 12:54:51 +0000 (13:54 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 6 Sep 2019 12:54:51 +0000 (13:54 +0100)
* 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
libstdc++-v3/doc/html/manual/status.html
libstdc++-v3/doc/xml/manual/status_cxx2020.xml
libstdc++-v3/include/bits/range_access.h
libstdc++-v3/testsuite/24_iterators/range_access_cpp20.cc [new file with mode: 0644]

index ad96489aa260293b615df5cf34e7b97561dea2ac..e135e850594733db5190f2a662f7feed2a76fb4b 100644 (file)
@@ -1,3 +1,10 @@
+2019-09-06  Jonathan Wakely  <jwakely@redhat.com>
+
+       * 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  <fweimer@redhat.com>
 
        * configure: Regenerate.
index 06269a7145579a93844eae4f2b666d50f7ed5d90..4442e195cc45de8565c3ac86983dc6f2d8e927eb 100644 (file)
@@ -1422,11 +1422,11 @@ Feature-testing recommendations for C++</a>.
         <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1001r2.html" target="_top">
         P1001R2
        </a>
-      </td><td align="center"> </td><td align="left"> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Signed ssize() functions, unsigned size() functions </td><td align="left">
+      </td><td align="center"> </td><td align="left"> </td></tr><tr><td align="left"> Signed ssize() functions, unsigned size() functions </td><td align="left">
         <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1227r2.html" target="_top">
         P1227R2
        </a>
-      </td><td align="center"> </td><td align="left"> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Ranges Design Cleanup </td><td align="left">
+      </td><td align="center"> 10.1 </td><td align="left"> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Ranges Design Cleanup </td><td align="left">
         <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1252r2.pdf" target="_top">
         P1252R2
        </a>
index ffd73aefc964b221d1accd5941df5f79595d2ca7..fa0c89dd346e52bb1d11e8b27a1529dcff5440a9 100644 (file)
@@ -1086,14 +1086,13 @@ Feature-testing recommendations for C++</link>.
     </row>
 
     <row>
-      <?dbhtml bgcolor="#C8B0B0" ?>
       <entry> Signed ssize() functions, unsigned size() functions </entry>
       <entry>
         <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1227r2.html">
         P1227R2
        </link>
       </entry>
-      <entry align="center"> </entry>
+      <entry align="center"> 10.1 </entry>
       <entry />
     </row>
 
index 44b9c6c3596a475571ea4a37950e3fe5fa46b402..c5744145590c7f636c0e216a1d31f4e3b949b99d 100644 (file)
@@ -319,6 +319,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif // C++17
 
 #if __cplusplus > 201703L
+  template<typename _Container>
+    constexpr auto
+    ssize(const _Container& __cont)
+    noexcept(noexcept(__cont.size()))
+    -> common_type_t<ptrdiff_t, make_signed_t<decltype(__cont.size())>>
+    {
+      using type = make_signed_t<decltype(__cont.size())>;
+      return static_cast<common_type_t<ptrdiff_t, type>>(__cont.size());
+    }
+
+  template<typename _Tp, ptrdiff_t _Num>
+    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 (file)
index 0000000..567b056
--- /dev/null
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// N4830 23.7, Range access [iterator.range]
+
+#include <iterator>
+
+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);
+}