220 30781 <a734e3fd-d203-1ed6-d605-bf54f6a6e1e9@wanadoo.fr> article
Path: news.gmane.org!.POSTED!not-for-mail
From: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: safe integrals comparison
Date: Sun, 5 Feb 2017 16:10:15 +0100
Lines: 106
Approved: news@gmane.org
Message-ID: <a734e3fd-d203-1ed6-d605-bf54f6a6e1e9@wanadoo.fr>
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: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: blaine.gmane.org 1486307418 22897 195.159.176.226 (5 Feb 2017 15:10:18 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sun, 5 Feb 2017 15:10:18 +0000 (UTC)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0)
 Gecko/20100101 Thunderbird/45.6.0
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDH67CONY4PBBWMA3XCAKGQE6CIFHQQ@isocpp.org Sun Feb 05 16:10:13 2017
Return-path: <std-proposals+bncBDH67CONY4PBBWMA3XCAKGQE6CIFHQQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lf0-f72.google.com ([209.85.215.72])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDH67CONY4PBBWMA3XCAKGQE6CIFHQQ@isocpp.org>)
	id 1caOSD-0005gy-DO
	for gclcip-std-proposals@m.gmane.org; Sun, 05 Feb 2017 16:10:13 +0100
Original-Received: by mail-lf0-f72.google.com with SMTP id z134sf25786739lff.5
        for <gclcip-std-proposals@m.gmane.org>; Sun, 05 Feb 2017 07:10:19 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=subject:to:references:from:message-id:date:user-agent:mime-version
         :in-reply-to:content-transfer-encoding:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=TApxkF4gDA0D0qmNYSQq0WLgyl8ychhBGpHGtHPWvlw=;
        b=Ih9ZXC8HHA+0UML4azj+lBWSQ4ebv+RHzOEvIsnLA0xzC8Ohej7OEJxRd0WgC8on9p
         +H6r4BgRG2y2VxP3ljPeJE/utAixxgmxyuFhCMS+ybQXPz0lGUVdUeX9/nmVQYv5ok19
         meCw50s16DwjH0kFfOybPkFa/mSJoqkyNEpmsdsul392Tso/fMydb2RkB7JsJ0lxLeLM
         xceOCJjzvWL/hFqEZHRDeQDe5fzky+G4tsbXCBZM97Jb1Acaa1UDNMuweEsNY11lOXna
         YqkJJndTRUX9zzg3WAYMsY6/m40T9rjWZNrg4Ur2R0OQIMf3iPanuyLqsAxa3sxkxCyI
      
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:subject:to:references:from:message-id:date
         :user-agent:mime-version:in-reply-to:content-transfer-encoding
         :x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=TApxkF4gDA0D0qmNYSQq0WLgyl8ychhBGpHGtHPWvlw=;
        b=ZStIyNFtncmR6gCIQhh2vUL9RvpBMPUguKMBX7PPrp3lWlssX7cPIY7eo8/7dPUOzq
         a3kwxrt3iqcymIKijVGtn8cjmtypzY1lEwgpe0fHdKHvZGMgZeOh5stutggfkMsicWB7
         aX+XcEPBAaPGO/2jDXCu9LHSC7KnIktviMC/p90meGxl67bIqydyPT28rAUKYIupzDIy
         LS03T1SP7Keufpg67yJRHMxkBc5FUblo4p/SjjqAqWkq/epBD2h98/5UdMArt7t4O/jW
         Sr2cF2OpX3BnWd4cfDFPhUc+BIYgZf3BW4gy0w5InEW8Vf1R9BEiFbYYVMzv5cCudBsA
  
X-Gm-Message-State: AIkVDXKLqUMlztuyVjGsK3yCGj18On3cyLf7Fi17r9fGsptmdC9Xo5YTRwTpNUyZ4haD7Q==
X-Received: by 10.46.80.86 with SMTP id v22mr557434ljd.22.1486307418394;
        Sun, 05 Feb 2017 07:10:18 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.159.195 with SMTP id i186ls806775wme.12.canary-gmail; Sun,
 05 Feb 2017 07:10:17 -0800 (PST)
X-Received: by 10.28.19.207 with SMTP id 198mr4869169wmt.70.1486307417251;
        Sun, 05 Feb 2017 07:10:17 -0800 (PST)
Original-Received: from smtp.smtpout.orange.fr (smtp13.smtpout.orange.fr. [80.12.242.135])
        by mx.google.com with ESMTPS id 140si4604234wmt.40.2017.02.05.07.10.17
        for <std-proposals@isocpp.org>
        (version=TLS1 cipher=AES128-SHA bits=128/128);
        Sun, 05 Feb 2017 07:10:17 -0800 (PST)
Received-SPF: neutral (google.com: 80.12.242.135 is neither permitted nor denied by best guess record for domain of vicente.botet@wanadoo.fr) client-ip=80.12.242.135;
Original-Received: from imac-de-vicente-botet-escriba.home ([2.11.72.89])
	by mwinf5d75 with ME
	id h3AF1u00R1vahZu033AGQW; Sun, 05 Feb 2017 16:10:17 +0100
X-ME-Helo: imac-de-vicente-botet-escriba.home
X-ME-Auth: dmljZW50ZS5ib3RldEB3YW5hZG9vLmZy
X-ME-Date: Sun, 05 Feb 2017 16:10:17 +0100
X-ME-IP: 2.11.72.89
In-Reply-To: <66f9bab2-7220-4bf1-afb7-77c5efa1bac3@isocpp.org>
X-Original-Sender: vicente.botet@wanadoo.fr
X-Original-Authentication-Results: mx.google.com;       spf=neutral
 (google.com: 80.12.242.135 is neither permitted nor denied by best guess
 record for domain of vicente.botet@wanadoo.fr) smtp.mailfrom=vicente.botet@wanadoo.fr
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:30781
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30781>

Le 20/01/2017 =C3=A0 17:31, federico.kircheis@gmail.com a =C3=A9crit :
> Hello to everyone, this is my first proposal, I hope you'll find it=20
> interesting, give me some feedback and help to improve my=20
> idea/implementation/proposal.
>
>
> <snip>
>
> My proposal would be to add four templated functions which work only=20
> for integral types (bool excluded).
>
> The first function would be:
>
> template <typename R, typename T> constexpr bool in_range(const T t)=20
> noexcept;
>
> Usage
> size_t i =3D=3D ...
> if(in_range<DWORD>(i)){
>     // safe to convert i to a DWORD value, parameter...
> } else {
>     // not possible to represent i as a DWORD
> }
>
There si something related in GSL narrow/narrow_cast. Your function=20
could be named can_be_narrowed<DWORD>(i).
There could also be

template <class T, class U>
bool can_be_narrowed(U);

template <class T, class U>
optional<T> try_to_narrow(U);
> The second function would be:
>
> template <typename T, typename U> constexpr bool cmp_equal(const T t,=20
> const U u) noexcept;
>
> Usage
> size_t i =3D=3D ...
> DWORD j =3D=3D ...
> if(cmp_equal(i,j)){
>     // i and j represent the same quantity
> } else {
>     // i and j represents different quantities
> }
>
>
Currently we are going more towards 3-value comparisons (p0100r2). There=20
will surely be an additional operator <=3D>. It would be better to start=20
by defining some kind of safe_3compare.

Have you considered defining a wrapper that has this safe semantic.

if( safe(i) =3D=3D safe(j) ){


> <snip>
>

> The fourth function I would like to add is:
>
> template <typename T> constexpr std::size_t precision() noexcept;
>
>
> The reasons are described here:
>
> https://www.securecoding.cert.org/confluence/display/c/INT35-C.+Use+corre=
ct+integer+precisions
>
> Quote:
> "Integer types in C have both a size and a precision. The size=20
> indicates the number of bytes used by an object and can be retrieved=20
> for any object or type using the sizeof operator.  The precision of an=20
> integer type is the number of bits it uses to represent values,=20
> excluding any sign and padding bits.
>
> Padding bits contribute to the integer's size, but not to its=20
> precision. Consequently, inferring the precision of an integer type=20
> from its size may result in too large a value, which can then lead to=20
> incorrect assumptions about the numeric range of these types. =20
> Programmers should use correct integer precisions in their code, and=20
> in particular, should not use the sizeof operator to compute the=20
> precision of an integer type on architectures that use padding bits or=20
> in strictly conforming (that is, portable) programs."
>
>
> The function precision is used internally (by cmp_equal, cmp_less and=20
> in_range) for verifying which type is bigger, in order to safely cast=20
> before comparing the types. I also think, that this function should be=20
> part of the API, since it is very handy.
Wondering if you shouldn't use std::numeric_limits<T>, and if there is=20
not enough information there to request the addition of a trait?

Vicente

--=20
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 e=
mail 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/a734e3fd-d203-1ed6-d605-bf54f6a6e1e9%40wanadoo.f=
r.

.
