From patchwork Fri Sep 6 12:54:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1158986 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-508490-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="VOnVfzcn"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46PyFg63rxz9s00 for ; Fri, 6 Sep 2019 22:55:02 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=kiRjC+tiTx34RQa82QVQy91IRD3bisqVqPT1Z9/OtTW6HHtWn7lRU saym5R3E9A0VRPrdyogG0v6vZdDeCQRDmeKQf1ytGYkgqSSj4LOXRbXaXNeeBuV2 TJLinBgvRL7Mon0LWXLO5gCPUc4FFH3oiZCIX3qiS06D524EOND1MQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=kP3zAwcTLJyw67Wckt7TxAAz/qI=; b=VOnVfzcnTiTibDa1RMcU UOICsUSeHAeWOAmqDsbTiA923PBvRKJVokun1Q37drTfkTHleKmpUG0WFSznWFoQ RcdzyBS94bvlBDNC/BgCQur9v/UgIBbTbD0tdg+ryHDM+VWZs2vguyN5iJtVRUCY ptJYa6Ys70/PfYyTQJcoLIk= Received: (qmail 742 invoked by alias); 6 Sep 2019 12:54:48 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 726 invoked by uid 89); 6 Sep 2019 12:54:48 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 06 Sep 2019 12:54:46 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 93D843084288; Fri, 6 Sep 2019 12:54:45 +0000 (UTC) Received: from localhost (unknown [10.33.36.99]) by smtp.corp.redhat.com (Postfix) with ESMTP id 412865D9E5; Fri, 6 Sep 2019 12:54:45 +0000 (UTC) Date: Fri, 6 Sep 2019 13:54:44 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Define std::ssize for C++20 (P1227R2) Message-ID: <20190906125444.GA8335@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.12.0 (2019-05-25) * 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. commit 606b6138a44f1f31577f480f37a3d263dd2bfc65 Author: Jonathan Wakely 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++. - 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 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 + 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 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 +// . + +// 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); +}