public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [PATCH] Define std::ssize for C++20 (P1227R2)
Date: Fri, 06 Sep 2019 12:54:00 -0000	[thread overview]
Message-ID: <20190906125444.GA8335@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 249 bytes --]

	* 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.

Tested x86_64-linux, committed to trunk.



[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 4175 bytes --]

commit 606b6138a44f1f31577f480f37a3d263dd2bfc65
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Sep 6 13:31:15 2019 +0100

    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.

diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2020.xml b/libstdc++-v3/doc/xml/manual/status_cxx2020.xml
index ffd73aefc96..fa0c89dd346 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++</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>
 
diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h
index 44b9c6c3596..c5744145590 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<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
index 00000000000..567b0564507
--- /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
+// <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);
+}

                 reply	other threads:[~2019-09-06 12:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190906125444.GA8335@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).