220 32898 <aa356514-dd5d-4210-8882-ffcef3ae1449@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Mingxin Wang <wmx16835vv@163.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: safe integrals comparison
Date: Mon, 26 Jun 2017 22:05:43 -0700 (PDT)
Lines: 143
Approved: news@gmane.org
Message-ID: <aa356514-dd5d-4210-8882-ffcef3ae1449@isocpp.org>
References: <66f9bab2-7220-4bf1-afb7-77c5efa1bac3@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_711_1432126139.1498539944103"
X-Trace: blaine.gmane.org 1498539948 15728 195.159.176.226 (27 Jun 2017 05:05:48 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 27 Jun 2017 05:05:48 +0000 (UTC)
Cc: federico.kircheis@gmail.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDNMBNHJWIGBBKGPY7FAKGQEZTQKY6I@isocpp.org Tue Jun 27 07:05:44 2017
Return-path: <std-proposals+bncBDNMBNHJWIGBBKGPY7FAKGQEZTQKY6I@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-io0-f200.google.com ([209.85.223.200])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDNMBNHJWIGBBKGPY7FAKGQEZTQKY6I@isocpp.org>)
	id 1dPih2-0003d1-QK
	for gclcip-std-proposals@m.gmane.org; Tue, 27 Jun 2017 07:05:41 +0200
Original-Received: by mail-io0-f200.google.com with SMTP id b204sf12558862ioe.11
        for <gclcip-std-proposals@m.gmane.org>; Mon, 26 Jun 2017 22:05:46 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=bI82zDiG8SV1KC+eGGL1hX9BPIQx0b5h0g0ON7kYOl8=;
        b=mm2J8n7v43y2kBS6TwJNQs7PZze8GjpCnv9F1LcaQ3XbAF3xeXx1yK3cuPk9JfLFIl
         jHKC+CLQ8p6/4FZ4l9EKIT8Dyi843TwYNEyzX+AzPd7DvrF+zzG8nXt4FPYLBe2mqeWj
         3Yi3b/K/h/izsUaELrHNai/zyyVvQaCY3d4wveqUgGnLrTXLcu9WxwDAHMK3sVbFNRHv
         95yyMJPLdXbxA3OAg9icXIfvtG51skN3TGpqkl7O2johyMOPS8Ys+QesFLVdY/n4Bh9D
         c9d8jBDuccdnLyntCpOWHJ1sPELXgf9A14ecmf8d4h9uql/UGLgiiCZW4M2b18O0jvA/
         CpgA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:cc:message-id:in-reply-to
         :references:subject:mime-version:x-original-sender:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=bI82zDiG8SV1KC+eGGL1hX9BPIQx0b5h0g0ON7kYOl8=;
        b=S67b063yZhblt5mCh89TVMP4KcLQ9nAgO9W/pLhYMfeckmAJafJeRQsrzXrUuyn2uZ
         /yOUj22wNNWegyGTq2rd6PET7yzQhRDeYSY8FgjB11kRtEELKH2r/5ksMF9SDhSUq7na
         QvRkBR/JcWQXSiUqq9/Gu0QDJv6f9WHMX0Mq7/iccvYTKuTzEboZDVKNpRFHX9vbHbn7
         NvvsJvVvslYluDKxrPUYnEiZusiSg0yw8ZTsb+b+p0ikRY6d6do6oXaou2xei99aUk3E
         BwiadULk1T16CMbv5HPVHk+jj3f/cfpKWKJCDcqRValg6xIfHTojDfSfGrqUbL6SBJKB
         CdtQ==
X-Gm-Message-State: AKS2vOz2DeacmbsF7lUEvFVYSy3OrNw53fOohHjies82P2CKPhr9d2ac
	zcjOeDgism6S6+oL
X-Received: by 10.107.58.87 with SMTP id h84mr1938174ioa.86.1498539945607;
        Mon, 26 Jun 2017 22:05:45 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.46.175 with SMTP id w44ls14227114ota.35.gmail; Mon, 26 Jun
 2017 22:05:44 -0700 (PDT)
X-Received: by 10.157.51.175 with SMTP id u47mr70001otc.4.1498539944647;
        Mon, 26 Jun 2017 22:05:44 -0700 (PDT)
In-Reply-To: <66f9bab2-7220-4bf1-afb7-77c5efa1bac3@isocpp.org>
X-Original-Sender: wmx16835vv@163.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:32898
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/32898>

------=_Part_711_1432126139.1498539944103
Content-Type: multipart/alternative; 
	boundary="----=_Part_712_416217759.1498539944104"

------=_Part_712_416217759.1498539944104
Content-Type: text/plain; charset="UTF-8"

This is a good idea! However, I think maybe the following issues shall be 
considered.

*This feature may introduce unnecessary extra overhead*

Comparing to bare comparators between signed and unsigned integral types, 
this feature requires *one extra comparation* between the signed one and 
ZERO at runtime. Actually, it is not always necessary in some cases, e.g.

std::vector<int> v;
for (int i = 0; i < v.size(); ++i) {
  // ...
}

Although variable i is a signed integer, the comparation is always 
executed correctly providing v.size() won't 
exceed std::numeric_limits<int>::max(). Thus the extra comparation 
introduced in your solution is redundant as we can assert that i is always 
positive. Still, I think there is enough motivation for us to have this 
feature.

*A uniform wrapper*

Although the problem can be solved with a uniform wrapper, that would 
introduce even much overhead, especially when there are implementation 
defined integral types, e.g. __int128 in GCC.

*Implementation with Concepts TS*

I think the implementation could be much simpler with Concepts TS. For 
instance, we can define different overloads for the function template 
cmp_less with different constraints, as is shown below:

template <class T, class U>
bool cmp_less(const T&, const U&); // undefined

template <class T, class U>
bool cmp_less(const T& lhs, const U& rhs) requires std::is_signed_v<T> == 
std::is_signed_v<U> {
  return lhs < rhs;
}

template <class T, class U>
bool cmp_less(const T& lhs, const U& rhs) requires std::is_signed_v<T> && 
!std::is_signed_v<U> {
  return lhs < 0 ? true : lhs < rhs;
}

template <class T, class U>
bool cmp_less(const T& lhs, const U& rhs) requires !std::is_signed_v<T> && 
std::is_signed_v<U> {
  return rhs < 0 ? false : lhs < rhs;
}

I hope these would help.

Mingxin Wang

-- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/aa356514-dd5d-4210-8882-ffcef3ae1449%40isocpp.org.

------=_Part_712_416217759.1498539944104
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">This is a good idea! However, I think maybe the following =
issues shall be considered.<div><br></div><div><b>This feature=C2=A0may int=
roduce unnecessary extra overhead</b></div><div><br></div><div>Comparing to=
 bare comparators between signed and unsigned integral types, this feature =
requires <i>one extra comparation</i> between the signed one and ZERO at ru=
ntime. Actually, it is not always necessary in some cases, e.g.</div><div><=
br></div><div><div class=3D"prettyprint" style=3D"border: 1px solid rgb(187=
, 187, 187); word-wrap: break-word; background-color: rgb(250, 250, 250);">=
<code class=3D"prettyprint"><div class=3D"subprettyprint"><div class=3D"sub=
prettyprint"><div class=3D"subprettyprint">std::vector&lt;int&gt; v;</div><=
div class=3D"subprettyprint">for (int i =3D 0; i &lt; v.size(); ++i) {</div=
><div class=3D"subprettyprint">=C2=A0 // ...</div><div class=3D"subprettypr=
int">}</div></div></div></code></div><br>Although variable i is a signed in=
teger, the comparation is always executed=C2=A0correctly providing v.size()=
 won&#39;t exceed=C2=A0std::numeric_limits&lt;int&gt;::max(). Thus the extr=
a comparation introduced in your solution is redundant as we can assert tha=
t i is always positive. Still, I think there is enough motivation for us to=
 have this feature.</div><div><br></div><div><b>A uniform wrapper</b></div>=
<div><br></div><div>Although the problem can be solved with a uniform wrapp=
er, that would introduce even much overhead, especially when there are impl=
ementation defined integral types, e.g.=C2=A0__int128 in GCC.</div><div><br=
></div><div><b>Implementation with Concepts TS</b></div><div><br></div><div=
>I think the implementation could be much simpler with Concepts TS. For ins=
tance, we can define different overloads for the function template cmp_less=
 with different constraints, as is shown below:</div><div><br></div><div><d=
iv class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); wor=
d-wrap: break-word; background-color: rgb(250, 250, 250);"><code class=3D"p=
rettyprint"><div class=3D"subprettyprint"><font color=3D"#660066"><div clas=
s=3D"subprettyprint">template &lt;class T, class U&gt;</div><div class=3D"s=
ubprettyprint">bool cmp_less(const T&amp;, const U&amp;); // undefined</div=
><div class=3D"subprettyprint"><br></div><div class=3D"subprettyprint">temp=
late &lt;class T, class U&gt;</div><div class=3D"subprettyprint">bool cmp_l=
ess(const T&amp; lhs, const U&amp; rhs) requires std::is_signed_v&lt;T&gt; =
=3D=3D std::is_signed_v&lt;U&gt; {</div><div class=3D"subprettyprint">=C2=
=A0 return lhs &lt; rhs;</div><div class=3D"subprettyprint">}</div><div cla=
ss=3D"subprettyprint"><br></div><div class=3D"subprettyprint">template &lt;=
class T, class U&gt;</div><div class=3D"subprettyprint">bool cmp_less(const=
 T&amp; lhs, const U&amp; rhs) requires std::is_signed_v&lt;T&gt; &amp;&amp=
; !std::is_signed_v&lt;U&gt; {</div><div class=3D"subprettyprint">=C2=A0 re=
turn lhs &lt; 0 ? true : lhs &lt; rhs;</div><div class=3D"subprettyprint">}=
</div><div class=3D"subprettyprint"><br></div><div class=3D"subprettyprint"=
>template &lt;class T, class U&gt;</div><div class=3D"subprettyprint">bool =
cmp_less(const T&amp; lhs, const U&amp; rhs) requires !std::is_signed_v&lt;=
T&gt; &amp;&amp; std::is_signed_v&lt;U&gt; {</div><div class=3D"subprettypr=
int">=C2=A0 return rhs &lt; 0 ? false : lhs &lt; rhs;</div><div class=3D"su=
bprettyprint">}</div></font></div></code></div></div><div><br></div><div>I =
hope these would help.</div><div><br></div><div>Mingxin Wang</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/aa356514-dd5d-4210-8882-ffcef3ae1449%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/aa356514-dd5d-4210-8882-ffcef3ae1449=
%40isocpp.org</a>.<br />

------=_Part_712_416217759.1498539944104--

------=_Part_711_1432126139.1498539944103--

.
