220 28997 <2f77b3f0-eb09-537d-6590-59f4d68875c3@gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Andrey Semashev <andrey.semashev@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: uint16_t data =x= static_cast<uint16_t>(1) + 1;
Date: Thu, 20 Oct 2016 03:06:22 +0300
Lines: 39
Approved: news@gmane.org
Message-ID: <2f77b3f0-eb09-537d-6590-59f4d68875c3@gmail.com>
References: <47e7f79f-8676-4a63-b7d3-09d64219d575@isocpp.org>
 <1672903.oiJgXq1YU5@tjmaciei-mobl1>
 <f7ec0f51-3d35-464f-b18f-d27f27d35ac3@isocpp.org>
 <3106991.qNzeP8ykmy@tjmaciei-mobl1>
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 1476921998 16912 195.159.176.226 (20 Oct 2016 00:06:38 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Thu, 20 Oct 2016 00:06:38 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.3.0
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCXY5TXHWYJRBAEVUDAAKGQEA2LTF5I@isocpp.org Thu Oct 20 02:06:33 2016
Return-path: <std-proposals+bncBCXY5TXHWYJRBAEVUDAAKGQEA2LTF5I@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lf0-f69.google.com ([209.85.215.69])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCXY5TXHWYJRBAEVUDAAKGQEA2LTF5I@isocpp.org>)
	id 1bx0sJ-0002VF-57
	for gclcip-std-proposals@m.gmane.org; Thu, 20 Oct 2016 02:06:23 +0200
Original-Received: by mail-lf0-f69.google.com with SMTP id n3sf18987210lfn.5
        for <gclcip-std-proposals@m.gmane.org>; Wed, 19 Oct 2016 17:06:25 -0700 (PDT)
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=pSFu85iHrpGn2eFs6Oi7eg8zQoYJXnnXp8DG61uQ+ZE=;
        b=dJA9jAb89nWZCCJ3nuKY+aZRR5UX+JTr7qRtTDvNBwcfgbMXMlKaNosmjzkomBEYpl
         s2FqiMxTltEVJOB+b0VXkUPx/DM2H1mWxsIo9slL/dTlmAyHFxiBrBe9JK5EUJSTGL0v
         rWG5ieklbPVdr0TMwa5zRQ2WT+bORAtweClJlO2OgnugFQgzbfoiNZvABML0B6imAPbJ
         /mcECZpUmjRtDFc76akQv+uCjw1rOI6yTr3nxkzfouGcriaxZubNwmnTJ+wEpaZP2Sev
         Q4C8yv7ZFbAt5k6dRNe2aV8Bf2jSK5hiOboC3XmEgd3COqLgsS8HAr9QPd5WyaFd3bID
      
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        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=pSFu85iHrpGn2eFs6Oi7eg8zQoYJXnnXp8DG61uQ+ZE=;
        b=cej0HXiZrzdr3ZDrBWrTnkmQfnnvAqV/q3AyR9yEwPXLO/zLEuA/p/qcO8LXoQpT4W
         N0re8t1wF2oEHtRZFYwaw75Lw3/dhIX8QBqugXNXivRxi717PSmR/+IUUQufTxh0tVF8
         hlDs2yvjMalHBolJT7wDZ2LGKQaoI5mVriqULqlEha/gdpY/bdszDq2V59sZS35iB0AQ
         RlwrIeLFAmIsII/9MshabyNTU86p5ErCM+FlBRFtbTxKbMRonibTxOL9DPwXeIAu7NmH
         Uhodadzut+egmhcxPWBjW7AuAgyJX0Kw6778DGOrOPfFOpQexdA4RgP5N36I9jVgLIC3
  
X-Gm-Message-State: AA6/9RlOWG2AZsigBWIqH+cpxHWXZSoOeGOTNvhf+0RC7RvQwyNWu5ijjGqlZcJVJDPkfQ==
X-Received: by 10.25.221.130 with SMTP id w2mr597826lfi.8.1476921985647;
        Wed, 19 Oct 2016 17:06:25 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.25.22.78 with SMTP id m75ls461807lfi.20.gmail; Wed, 19 Oct
 2016 17:06:24 -0700 (PDT)
X-Received: by 10.25.132.18 with SMTP id g18mr8552961lfd.7.1476921984505;
        Wed, 19 Oct 2016 17:06:24 -0700 (PDT)
Original-Received: from mail-lf0-x241.google.com (mail-lf0-x241.google.com. [2a00:1450:4010:c07::241])
        by mx.google.com with ESMTPS id o102si5676687lfi.9.2016.10.19.17.06.24
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Wed, 19 Oct 2016 17:06:24 -0700 (PDT)
Received-SPF: pass (google.com: domain of andrey.semashev@gmail.com designates 2a00:1450:4010:c07::241 as permitted sender) client-ip=2a00:1450:4010:c07::241;
Original-Received: by mail-lf0-x241.google.com with SMTP id l131so4609433lfl.0
        for <std-proposals@isocpp.org>; Wed, 19 Oct 2016 17:06:24 -0700 (PDT)
X-Received: by 10.25.89.138 with SMTP id n132mr8620035lfb.68.1476921983843;
        Wed, 19 Oct 2016 17:06:23 -0700 (PDT)
Original-Received: from [192.168.1.2] (broadband-90-154-68-136.nationalcablenetworks.ru. [90.154.68.136])
        by smtp.googlemail.com with ESMTPSA id 84sm11378518ljf.33.2016.10.19.17.06.22
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Wed, 19 Oct 2016 17:06:23 -0700 (PDT)
In-Reply-To: <3106991.qNzeP8ykmy@tjmaciei-mobl1>
X-Original-Sender: andrey.semashev@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com;       spf=pass (google.com: domain of
 andrey.semashev@gmail.com designates 2a00:1450:4010:c07::241 as permitted
 sender) smtp.mailfrom=andrey.semashev@gmail.com;       dmarc=pass (p=NONE
 dis=NONE) header.from=gmail.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:28997
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/28997>

On 10/20/16 02:02, Thiago Macieira wrote:
> Em quarta-feira, 19 de outubro de 2016, =C3=A0s 13:07:31 PDT, Myriachan e=
screveu:
>> On Wednesday, October 19, 2016 at 11:09:17 AM UTC-7, Thiago Macieira wro=
te:
>>> Actually, the compiler is required to perform the cast to int, due to
>>> integer
>>> promotion rules. But the operation doesn't change either way.
>>
>> The promotion to int is also very important to consider in avoiding a
>> certain case of undefined behavior:
>>
>> std::uint16_t x =3D 0xFFFF;
>> x *=3D x;
>
> Indeed, this causes a signed integer overflow, which is UB.
>
> If you'd written:
>
> 	x *=3D unsigned(x);
>
> Then this is no longer UB.

I've always wondered why the language had allowed the implicit=20
conversion to int in such cases. I mean, if it didn't the implementation=20
could have compiled the code to use full-width 32-bit instructions and=20
then slice the result and have the same performance anyway. But that=20
wouldn't introduce problems like in the OP or the UB above.

--=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/2f77b3f0-eb09-537d-6590-59f4d68875c3%40gmail.com=
..

.
