220 32993 <4473c388-1bc0-24b3-b1f2-fc8bfc834df3@rrsd.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Robert Ramey <ramey@rrsd.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: safe integrals comparison
Date: Wed, 28 Jun 2017 14:47:17 -0700
Lines: 182
Approved: news@gmane.org
Message-ID: <4473c388-1bc0-24b3-b1f2-fc8bfc834df3@rrsd.com>
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
X-Trace: blaine.gmane.org 1498686450 29763 195.159.176.226 (28 Jun 2017 21:47:30 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Wed, 28 Jun 2017 21:47:30 +0000 (UTC)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0)
 Gecko/20100101 Thunderbird/52.2.1
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDG73K7L6AIRB3WH2DFAKGQE7MME6MQ@isocpp.org Wed Jun 28 23:47:24 2017
Return-path: <std-proposals+bncBDG73K7L6AIRB3WH2DFAKGQE7MME6MQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yb0-f200.google.com ([209.85.213.200])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDG73K7L6AIRB3WH2DFAKGQE7MME6MQ@isocpp.org>)
	id 1dQKnz-0007Ml-D5
	for gclcip-std-proposals@m.gmane.org; Wed, 28 Jun 2017 23:47:23 +0200
Original-Received: by mail-yb0-f200.google.com with SMTP id u67sf53376855yba.6
        for <gclcip-std-proposals@m.gmane.org>; Wed, 28 Jun 2017 14:47:28 -0700 (PDT)
ARC-Seal: i=2; a=rsa-sha256; t=1498686448; cv=pass;
        d=google.com; s=arc-20160816;
        b=A/YqtdaCLXa9T5yJTsuJUp5Ii/jzTh14cX8TRzrfYo8E6o8hYXYbeRbgBBxkcG+5uw
         T98WvuvlCVU5owO5LXNdqhBQZyMzVjSrsRW4i/sbjb2bUcWYYc8bdaeaRLenCNfymvTX
         CK8LB4s/a1kEPVp3Y6/n3Q53yY+EzpqJtMK6hzX05+xE1AlnFAIZ0cZqijyt+EQxmCt9
         hZqr1j8zgtDT4g2oaHChc94S3WZyHzfJRPktzdghgMgb3Js94dIS1aBDfwdNA+bxhope
         tn+4LK4YE/XOxfkwezmBWyIcuF8NCQsfQh7gqidWMdAroP/2U+zDR+CLLhMQIjqNWnlp
         U63g==
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post
         :list-id:mailing-list:precedence:reply-to:content-language
         :in-reply-to:mime-version:user-agent:date:message-id:from:references
         :newsgroups:to:subject:arc-authentication-results
         :arc-message-signature:dkim-signature:arc-authentication-results;
        bh=ezT/uu3w0tM/p07TI48P6c3JSGP+WdOwdxhVk+nyDlw=;
        b=lShVwcWhiNIQ1DdPVSTSBBXtRofLcy2SnUAmCipuClqr1ZQexpc33F0d2zYh/11T5/
         JRr7xqEpQEAeChwZDU3oO9NNA53N2sSvDRU5QRmrQV7/ZKfwjS8OzVt1TsCHLmRSG/KW
         6sNRFJBSoi5YcMdHr+nCSJmjp87UdBOw/WiSvt3KI8JlIYcYosgrpx8MvwAo9WpDlbCD
         53bNVVyNV1d/QHr18P1X1Ow/nxFUJqY47M11YZa8NvoHXfR0huFzh6KGSiGW49d4hB02
         AsV1V3Y9ymFWKGzD75w7gZhrpwpq8cczuTP7sYJRgrKttyP1dWGPxnaiocV51L1EW0a/
         
ARC-Authentication-Results: i=2; mx.google.com;
       spf=pass (google.com: domain of ramey@rrsd.com designates 23.83.222.31 as permitted sender) smtp.mailfrom=ramey@rrsd.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=subject:to:newsgroups:references:from:message-id:date:user-agent
         :mime-version:in-reply-to:content-language:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=ezT/uu3w0tM/p07TI48P6c3JSGP+WdOwdxhVk+nyDlw=;
        b=2LTsyjQzka8b+iFkUhBRWEPvnzWmrGlkGOjX+rwhPGWRIAX3vnWbhDar6/9OJ0Yow3
         /7cnxFCP9hFh8s2tdvFjyyQZmj5uqoH7dYhoz4rv23pesThcb3BN4WP4jr5p/9K5V8Nd
         qmFGbYpq18XynHIjNpRUz3pODEatjrXz/N/DzYFmrezDjhXK343Zm9FO2Biq7Xb2zrYC
         h1OL4f3eFf4br2VQryiuBl3PLJV42AO+sIt2DiV5QHmNA40e7rfphDbhVMQmyhzbivgv
         ZwBMIHzXV01/P2HIyYv5zMndlZM9CCS3BDj6SwRFUS5yC2CEKkbRJtIUNuab4QX/Mynq
         y4zw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:subject:to:newsgroups:references:from:message-id
         :date:user-agent:mime-version:in-reply-to:content-language
         :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=ezT/uu3w0tM/p07TI48P6c3JSGP+WdOwdxhVk+nyDlw=;
        b=ulVKmZL8IAwkhW2HEZdTDAN9U5uMclk8AsIlla7pggOKrUovC0AEeOz8gMGW78bwJx
         fDOy9KJ7s9D9u1wOAqLektXBYy4/GtJSBAPvWMmwM+r77SnManP0Td1lbIoTuaBBRu0K
         Ue0zzSwIfgjGsjytgPoqWaOPB4AyUgesnPgvFussfoKUUPc1SUERLpRIaO8R1rKidpET
         Ft2kdQojEJuf7uJ6FhVNgaC36A1nyMkvhF7kvGsxycIxfiajEVB+djDkk6BVdTlCGe5t
         zNidXmHItg1PDn3pthk9DQLeSOUXAcxha79e8u+a5NP0qJAspyBeIrmolFoQ91IT4Q+3 
X-Gm-Message-State: AKS2vOyFI8gsHD7WUxMCsVXUgN4aa5Pwcg8GkOavXqE0hTe8UhkrGj6c
	MqV9kTjHMuWOIAml
X-Received: by 10.129.145.204 with SMTP id i195mr7432366ywg.35.1498686448155;
        Wed, 28 Jun 2017 14:47:28 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.181.22 with SMTP id e22ls6055369iof.47.gmail; Wed, 28 Jun
 2017 14:47:26 -0700 (PDT)
X-Received: by 10.84.194.165 with SMTP id h34mr13893229pld.65.1498686446554;
        Wed, 28 Jun 2017 14:47:26 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1498686446; cv=none;
        d=google.com; s=arc-20160816;
        b=dsxJe+UmUBfNhNSEf7/AxiTyryx0VSekZ7QNXoUhF0JTWQPnS/j5l6vAba65bhf6f3
         1evMhXyqa2or5VRUFe5x4SUC1OvbZTZVbO5ivDX3kTYannuIm3urcyQikOZjlXYQOF3t
         bAPtsFtcjg/IagI+Tt3gYuoR+7uSHa3wsO8wCQwxIilDE3iYmO+AO58RmP8hxfcP9LL1
         v/ZRcsHfjCNVmaGhNiiIl3m9QlCrhnowF8Vy/wYKfFKKpyjo/CZyUek1gdfnmNd5BmxB
         5MbVFMCVWB/AbavcAs9FO1lKKQ5nm+V3gZ/JQvu7e99O1a1wP37JrwC/8/veXKNIN75X
         a39Q==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=content-transfer-encoding:content-language:in-reply-to:mime-version
         :user-agent:date:message-id:from:references:newsgroups:to:subject
         :arc-authentication-results;
        bh=7BZzk3RnHx3Dq+pmmF8dH/cLh3t47q4/rYoOwuAnnEs=;
        b=fPosDagiv907Tw5x34xKldgK4mTF7hp9Md4x0IPPgqiCNMahspk9bTaPNWygw1EOGC
         JglHewrCK+GwFfQzPPB2xi9SQeF6GwzkuktdPw3l23JmQo0HCCTPfJ9jctxaq10EtGDC
         MVQuXWXdyVkoLOaclccQk3KEYb2Y73ycRdW5ied2s7+SkcpGGbStbPfck9E0McZuxQ28
         g2qwvS+6H8lJxYDdGlvV2vhy1Mg9jnbPd5CJSRSedSH7mvHqPT3FRFrupz9XPICmweFV
         hFfb3TujflvpmQYrCMd477kHaIJE9wj7EjeZGBy/KRyh2f+6B0cqNeKGfqmnC/dT5xl7
         rg7g==
ARC-Authentication-Results: i=1; mx.google.com;
       spf=pass (google.com: domain of ramey@rrsd.com designates 23.83.222.31 as permitted sender) smtp.mailfrom=ramey@rrsd.com
Original-Received: from cat.ash.relay.mailchannels.net (cat.ash.relay.mailchannels.net. [23.83.222.31])
        by mx.google.com with ESMTPS id 12si2306282pfs.240.2017.06.28.14.47.24
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Wed, 28 Jun 2017 14:47:26 -0700 (PDT)
Received-SPF: pass (google.com: domain of ramey@rrsd.com designates 23.83.222.31 as permitted sender) client-ip=23.83.222.31;
X-Sender-Id: wwwh2|x-authuser|ramey@rrsd.com
Original-Received: from relay.mailchannels.net (localhost [127.0.0.1])
	by relay.mailchannels.net (Postfix) with ESMTP id 0FC895C7DB2
	for <std-proposals@isocpp.org>; Wed, 28 Jun 2017 21:47:21 +0000 (UTC)
Original-Received: from clippers.accountservergroup.com (unknown [100.96.141.35])
	(Authenticated sender: wwwh2)
	by relay.mailchannels.net (Postfix) with ESMTPA id 870285C6D44
	for <std-proposals@isocpp.org>; Wed, 28 Jun 2017 21:47:20 +0000 (UTC)
X-Sender-Id: wwwh2|x-authuser|ramey@rrsd.com
Original-Received: from clippers.accountservergroup.com ([UNAVAILABLE].
 [172.20.107.195])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA)
	by 0.0.0.0:2500 (trex/5.9.7);
	Wed, 28 Jun 2017 21:47:21 +0000
X-MC-Relay: Neutral
X-MailChannels-SenderId: wwwh2|x-authuser|ramey@rrsd.com
X-MailChannels-Auth-Id: wwwh2
X-Soft-Spill: 52188fc148c40727_1498686440848_1212665696
X-MC-Loop-Signature: 1498686440848:1364179264
X-MC-Ingress-Time: 1498686440848
Original-Received: from ip68-6-126-194.sb.sd.cox.net ([68.6.126.194]:54755 helo=Roberts-Mac-mini.local)
	by clippers.accountservergroup.com with esmtpsa (TLSv1:DHE-RSA-AES128-SHA:128)
	(Exim 4.87)
	(envelope-from <ramey@rrsd.com>)
	id 1dQKnv-0005Tp-2X
	for std-proposals@isocpp.org; Wed, 28 Jun 2017 14:47:19 -0700
Original-Newsgroups: gmane.comp.lang.c++.isocpp.proposals
In-Reply-To: <66f9bab2-7220-4bf1-afb7-77c5efa1bac3@isocpp.org>
Content-Language: en-US
X-AuthUser: ramey@rrsd.com
X-Original-Sender: ramey@rrsd.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of ramey@rrsd.com designates 23.83.222.31 as permitted sender) smtp.mailfrom=ramey@rrsd.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:32993
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/32993>

The following is what I use


#ifndef BOOST_NUMERIC_SAFE_COMPARE_HPP
#define BOOST_NUMERIC_SAFE_COMPARE_HPP

// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif

//  Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <type_traits>
#include <limits>

#include <boost/mpl/if.hpp>
#include <boost/mpl/identity.hpp>

#include "safe_common.hpp"

namespace boost {
namespace numeric {
namespace safe_compare {

////////////////////////////////////////////////////
// safe comparison on primitive types
namespace safe_compare_detail {
     template<typename T>
     using make_unsigned = typename boost::mpl::if_c<
         //std::numeric_limits<T>::is_signed,
         std::is_signed<T>::value,
         std::make_unsigned<T>,
         boost::mpl::identity<T>
     >::type;

     // both arguments unsigned or signed
     template<bool TS, bool US>
     struct less_than {
         template<class T, class U>
         constexpr static bool invoke(const T & t, const U & u){
             return t < u;
         }
     };

     // T unsigned, U signed
     template<>
     struct less_than<false, true> {
         template<class T, class U>
         constexpr static bool invoke(const T & t, const U & u){
             return
                 (u < 0) ?
                     false
                 :
                     less_than<false, false>::invoke(
                         t,
                         static_cast<const typename 
make_unsigned<U>::type &>(u)
                     )
                 ;
         }
     };
     // T signed, U unsigned
     template<>
     struct less_than<true, false> {
         template<class T, class U>
         constexpr static bool invoke(const T & t, const U & u){
             return
                 (t < 0) ?
                     true
                 :
                     less_than<false, false>::invoke(
                         static_cast<const typename 
make_unsigned<T>::type &>(t),
                         u
                     )
                 ;
         }
     };
} // safe_compare_detail

template<class T, class U>
constexpr bool less_than(const T & lhs, const U & rhs) {
     return safe_compare_detail::less_than<
         std::is_signed<T>::value,
         std::is_signed<U>::value
     >::template invoke(lhs, rhs);
}

template<class T, class U>
constexpr bool greater_than(const T & lhs, const U & rhs) {
     return less_than(rhs, lhs);
}

template<class T, class U>
constexpr bool less_than_equal(const T & lhs, const U & rhs) {
     return ! greater_than(lhs, rhs);
}

template<class T, class U>
constexpr bool greater_than_equal(const T & lhs, const U & rhs) {
     return ! less_than(lhs, rhs);
}

namespace safe_compare_detail {
     // both arguments unsigned or signed
     template<bool TS, bool US>
     struct equal {
         template<class T, class U>
         constexpr static bool invoke(const T & t, const U & u){
             return t == u;
         }
     };

     // T unsigned, U signed
     template<>
     struct equal<false, true> {
         template<class T, class U>
         constexpr static bool invoke(const T & t, const U & u){
             return
                 (u < 0) ?
                     false
                 :
                     equal<false, false>::invoke(
                         t,
                         static_cast<const typename 
make_unsigned<U>::type &>(u)
                     )
                 ;
         }
     };
     // T signed, U unsigned
     template<>
     struct equal<true, false> {
         template<class T, class U>
         constexpr static bool invoke(const T & t, const U & u){
             return
                 (t < 0) ?
                     false
                 :
                     equal<false, false>::invoke(
                         static_cast<const typename 
make_unsigned<T>::type &>(t),
                         u
                     )
                 ;
         }
     };
} // safe_compare_detail

template<class T, class U>
constexpr bool equal(const T & lhs, const U & rhs) {
     return safe_compare_detail::equal<
         std::numeric_limits<T>::is_signed,
         std::numeric_limits<U>::is_signed
     >::template invoke(lhs, rhs);
}

template<class T, class U>
constexpr bool not_equal(const T & lhs, const U & rhs) {
     return ! safe_compare_detail::equal<
         std::numeric_limits<T>::is_signed,
         std::numeric_limits<U>::is_signed
     >::template invoke(lhs, rhs);
}

} // safe_compare
} // numeric
} // boost

#endif // BOOST_NUMERIC_SAFE_COMPARE_HPP

-- 
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/4473c388-1bc0-24b3-b1f2-fc8bfc834df3%40rrsd.com.

.
