Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1286154pxk; Fri, 25 Sep 2020 10:43:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyaBdtyButtVuH6/qP+ahVs1r9376adQtSlB7G8JFFE44rgNvnmKSm9NtN5pyQDFHTLKAUR X-Received: by 2002:a17:906:6b0b:: with SMTP id q11mr3986040ejr.412.1601055784638; Fri, 25 Sep 2020 10:43:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601055784; cv=none; d=google.com; s=arc-20160816; b=tZZJjZoUi5/qMFbVJeroUSTxUMave55cDuyW/wrAF209obZt8hnK0DypJKveaTsEEn rCSyV8/wQ1g4dClHwDUYphMj4H3pQpT72r2cunsbztNdLPqak3mPJ9Mgt62UnQoc8KfI Rc34njDuYhRXviX9drQgNNkVFQOUypbEdt/Mg/QJS72frxOeVygf7rSwOD3bB4/1yF7w geGcBN0tMx9UFEzHtCsBf08tVx8QtIggGZu5cdmSUJA8Yui1cmRRXeCi4Yf1EaCpX36x TH3mNJYZ6YihWCG5wgnD9FnaWC40PHECzg3WSZhK0+sy22ETleF/9GktfZwkHEyOWRqE T8rg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=i0NUkfA/IECNtbw53+ZcCVAPJL6aLyJb1AKj19Cje3A=; b=mfRfOd/W6DQkZ5wwRoWNpS5U0vZ2FSTjyaVA7D6pRZypXY+/m+69QByK3EV9qQg8s/ Rp1UKtkKiUQlKc4gHbBVfhgNqjbURliXUZBwtd+jFcqo3oeDZnFabK/FS9K4VYiQa9Es SABgBx48uLHG9wEKXCkY0Bif8QlMIZWM9SSx4JCZGqbHC66i+aiVOgJoN9Y61qS0ssY4 TsbBH3cQtc6JUHZwJxpyoVJScPl9TA0gGq0F79vjZPSJCWhlOndMjuSJEw1V2i1MWjRT AkTOr3SBZp7ju4XcqVSq01lEhH11Hl/lPmqIM1Hw7dvoFeYAbr/0OkaX/HGF9xhN38V7 BiDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QuZfka1f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s10si2228924ejr.574.2020.09.25.10.42.40; Fri, 25 Sep 2020 10:43:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QuZfka1f; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729614AbgIYRkA (ORCPT + 99 others); Fri, 25 Sep 2020 13:40:00 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:44499 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727290AbgIYRkA (ORCPT ); Fri, 25 Sep 2020 13:40:00 -0400 Dkim-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601055598; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=i0NUkfA/IECNtbw53+ZcCVAPJL6aLyJb1AKj19Cje3A=; b=QuZfka1fZ4VMFfcXSr+WiyJUnVh0iZ2UVJiJb2lylujzLrKHAjzHWimj6uxyePEGpv0ubN s1fiWpp4AdKjU88RS+VGucJZKq48/5Bi1kASKcvoPg0dnaGCn/CtdB3v937zCNrgBMuP72 Go8IA9I6DWnNDVjm0yllWM7Tjypzsyk= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-289-dCpyZtn4ODenprkVglBi8A-1; Fri, 25 Sep 2020 13:39:56 -0400 X-MC-Unique: dCpyZtn4ODenprkVglBi8A-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 852511891E89; Fri, 25 Sep 2020 17:39:54 +0000 (UTC) Received: from localhost (unknown [10.33.36.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id B420D5C1C7; Fri, 25 Sep 2020 17:39:53 +0000 (UTC) Date: Fri, 25 Sep 2020 18:39:52 +0100 From: Jonathan Wakely To: Alejandro Colomar Cc: fweimer@redhat.com, linux-man@vger.kernel.org, libc-alpha@sourceware.org, gcc@gcc.gnu.org, rusty@rustcorp.com.au, linux-kernel@vger.kernel.org, libstdc++@gcc.gnu.org, libc-coord@lists.openwall.com, enh@google.com Subject: Re: [PATCH v2] : Add nitems() and snitems() macros Message-ID: <20200925173952.GN6061@redhat.com> References: <20200922145844.31867-1-colomar.6.4.3@gmail.com> <20200925132000.235033-1-colomar.6.4.3@gmail.com> <20200925144822.GM6061@redhat.com> <22c110fe-4c92-e5e6-dc35-dbf00a97cfa2@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <22c110fe-4c92-e5e6-dc35-dbf00a97cfa2@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 25/09/20 18:30 +0200, Alejandro Colomar via Libstdc++ wrote: >Hello Jonathan, > >On 2020-09-25 16:48, Jonathan Wakely wrote: >> Do you really need to provide snitems? >> >> Users can use (ptrdiff_t)nitems if needed, can't they? > >They can, but that adds casts in the code, >which makes longer lines that are somewhat harder to read. >To avoid that, users may sometimes omit the cast with possible UB. >BTW, I use > >IMO, array indices should be declared as 'ptrdiff_t' always, >and not 'size_t'. More generically, I use unsigned integer types for two >reasons: bitwise operations, and library functions that require me to >do so. > >I don't intend to force anyone with my opinion, of course, >but if I were to choose a type for 'nitems()', it would be 'ptrdiff_t'. > >However, for legacy reasons people will expect that macro to be unsigned, >so I'd have 'nitems()' unsigned, and then a signed version prefixed >with an 's'. > >Some very interesting links about this topic: > >Bjarne Stroustrup (and others) about signed and unsigned integers: >https://www.youtube.com/watch?v=Puio5dly9N8&t=12m56s >https://www.youtube.com/watch?v=Puio5dly9N8&t=42m41s > >The two links above are two interesting moments of the same video. > >I guess that might be the reason they added std::ssize, BTW. Yes, I'm aware of all the rationale. I already said that it makes sense in C++ where you have generic code. I am not convinced that it's necessary to add to when all it does is a cast from size_t to ptrdiff_t.