220 30548 <c8da1219-1d83-4812-f099-3b270d179ab7@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: Re: enum_cast proposal
Date: Fri, 13 Jan 2017 08:54:14 +0100
Lines: 334
Approved: news@gmane.org
Message-ID: <c8da1219-1d83-4812-f099-3b270d179ab7@wanadoo.fr>
References: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
 <57258435-5d82-48e7-8ed7-3c38682418fb@isocpp.org>
 <a35d9f20-9e4f-4789-b7e5-5cb08f99e6bd@isocpp.org>
 <e779f1ca-8f05-7eda-72c8-89a9a5215c62@wanadoo.fr>
 <24e58aad-ecbc-4c83-a21b-de56ea4c1db0@isocpp.org>
 <040e99e3-8f07-409f-3a47-ebdfb2bfb465@wanadoo.fr>
 <1841091b-9f97-4724-9f95-3193c0fb94fb@isocpp.org>
 <214358e3-699e-f804-4289-14472559a51a@wanadoo.fr>
 <dbe0b3d3-e7ea-426a-a78d-6681202a4ea7@isocpp.org>
 <0a4162ad-62ed-1c26-e210-81000402b193@wanadoo.fr>
 <d4065d2b-f8b7-4b55-ba43-65895c14e0bb@isocpp.org>
 <b2c10414-ec5d-9bc3-dd7e-48bff6f1fa5e@wanadoo.fr>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="------------F966C3143431216CDC3D0344"
X-Trace: blaine.gmane.org 1484294058 2140 195.159.176.226 (13 Jan 2017 07:54:18 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Fri, 13 Jan 2017 07:54:18 +0000 (UTC)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0)
 Gecko/20100101 Thunderbird/45.6.0
Cc: m.cencora@gmail.com, gmisocpp@gmail.com
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDH67CONY4PBBKMP4LBQKGQE6BWZ3SY@isocpp.org Fri Jan 13 08:54:13 2017
Return-path: <std-proposals+bncBDH67CONY4PBBKMP4LBQKGQE6BWZ3SY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wm0-f70.google.com ([74.125.82.70])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDH67CONY4PBBKMP4LBQKGQE6BWZ3SY@isocpp.org>)
	id 1cRwgf-00009N-Ml
	for gclcip-std-proposals@m.gmane.org; Fri, 13 Jan 2017 08:54:13 +0100
Original-Received: by mail-wm0-f70.google.com with SMTP id r144sf12961564wme.0
        for <gclcip-std-proposals@m.gmane.org>; Thu, 12 Jan 2017 23:54:18 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=subject:to:references:cc:from:message-id:date:user-agent
         :mime-version:in-reply-to: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=8Zx1gU1lUX/XkXTrSTZBeoDPIpq/kSFH0xXxwVvOGK0=;
        b=AO+fwNEUid2PAxsKJ3LZx1PXtXVSLa1J9CDT6ug1ug8sDeY0NejYgwo9vaZmK2oWP0
         nTDJaorP1Lfcy8QvrIquUzarx2XkOSjioQWg5be54M1+WSGwefqud1thu9BC8NCy3Bmb
         atGKx/q4g8wnQzsd9wXwAexKXZqivJYpMGq6J5lgPHEd9NrsOF8LpXhdSBSnADmHP1EA
         0UxaZ9NtWuQjkbF/bGi8ACdfFl9/Q6///NRY1ZNbIpUf5qZmgt7gAtgStwRkdoTnS5rf
         ONHVOz2aS8/Nx4S3Q9Hr26aMIoOdC9P3h3IbSjTI8qMSqACs2jVXGZckqxXyrNLcugm4
         ZB3Q==
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:cc:from:message-id:date
         :user-agent:mime-version:in-reply-to: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=8Zx1gU1lUX/XkXTrSTZBeoDPIpq/kSFH0xXxwVvOGK0=;
        b=lLLSNTeq5b/41xrRu/FbrREGS5LbDybg2uvf/p5q9vYXghRFQ0OQTr9eh8dZXAuOuQ
         sq5cUkCZ0G+qYKylw/aTLISpX20aAHD6LPObbQpS7XAx4d4znoJZO2CGrjcSRvyAJeK7
         VPdko1PCA1Zckt2QdvVh2VgbaJhxXNjTJ3uxiyh6xlB8W2/0PSfkVCFYMyFQpnYD4CGm
         rDfRzCUifI9P4EoudxS/uTr/Gj1AScU20kt1Ys8ve2IG/nCDDMO6CK27VJokWCb6BjyH
         XQpFUi2OAuJ1+3XyQiSHXfM5w+MmtIDvqUmMGgK143nhjb4wh8EwemCsi9KnqG+G5jMG
         OT8A==
X-Gm-Message-State: AIkVDXJaycltfzbQKfV1w0VzVypvmQPvFx/pReZ0YbVDEk9LTUVB6f2wL8KZ2dwmMscu2A==
X-Received: by 10.28.13.14 with SMTP id 14mr52621wmn.9.1484294058473;
        Thu, 12 Jan 2017 23:54:18 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.23.140 with SMTP id 134ls549630wmx.23.canary-gmail; Thu, 12
 Jan 2017 23:54:17 -0800 (PST)
X-Received: by 10.223.136.152 with SMTP id f24mr6140872wrf.187.1484294057311;
        Thu, 12 Jan 2017 23:54:17 -0800 (PST)
Original-Received: from smtp.smtpout.orange.fr (smtp01.smtpout.orange.fr. [80.12.242.123])
        by mx.google.com with ESMTPS id w62si1145878wme.143.2017.01.12.23.54.17
        for <std-proposals@isocpp.org>
        (version=TLS1 cipher=AES128-SHA bits=128/128);
        Thu, 12 Jan 2017 23:54:17 -0800 (PST)
Received-SPF: neutral (google.com: 80.12.242.123 is neither permitted nor denied by best guess record for domain of vicente.botet@wanadoo.fr) client-ip=80.12.242.123;
Original-Received: from imac-de-vicente-botet-escriba.home ([86.214.8.248])
	by mwinf5d02 with ME
	id XjuF1u0075M5Qem03juFmH; Fri, 13 Jan 2017 08:54:16 +0100
X-ME-Helo: imac-de-vicente-botet-escriba.home
X-ME-Auth: dmljZW50ZS5ib3RldEB3YW5hZG9vLmZy
X-ME-Date: Fri, 13 Jan 2017 08:54:16 +0100
X-ME-IP: 86.214.8.248
In-Reply-To: <b2c10414-ec5d-9bc3-dd7e-48bff6f1fa5e@wanadoo.fr>
X-Original-Sender: vicente.botet@wanadoo.fr
X-Original-Authentication-Results: mx.google.com;       spf=neutral
 (google.com: 80.12.242.123 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:30548
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30548>

This is a multi-part message in MIME format.
--------------F966C3143431216CDC3D0344
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 13/01/2017 =C3=A0 01:09, Vicente J. Botet Escriba a =C3=A9crit :
> Le 12/01/2017 =C3=A0 17:07, Nicol Bolas a =C3=A9crit :
>>
>>
>> On Thursday, January 12, 2017 at 2:28:00 AM UTC-5, Vicente J. Botet=20
>> Escriba wrote:
>>
>>     Le 11/01/2017 =C3=A0 22:21, Nicol Bolas a =C3=A9crit :
>>>     On Wednesday, January 11, 2017 at 1:52:11 PM UTC-5, Vicente J.
>>>     Botet Escriba wrote:
>>>
>>>         Le 11/01/2017 =C3=A0 00:06, Nicol Bolas a =C3=A9crit :
>>>>         On Tuesday, January 10, 2017 at 5:26:11 PM UTC-5, Vicente
>>>>         J. Botet Escriba wrote:
>>>>
>>>>         The question I have to ask is this: if you have an integer,
>>>>         why do you need to know if it will fit within the range of
>>>>         an enum with an implied underlying type?
>>>         Because initializing the enum with an integer out of range
>>>         is UB.
>>>
>>>
>>>     You're misunderstanding my question.
>>>
>>>     The situation you describe is one where:
>>>
>>>     1. You have an integer of arbitrary origin.
>>>
>>>     2. You want to convert it to an enum type.
>>>
>>>     3. That integer /does not match/ one of the enumerators in that
>>>     type.
>>>
>>>     4. The enum type does not have a fixed underlying type.
>>>
>>     You have described very well the context.
>>>     What goal are you trying to achieve with all this? Or more to
>>>     the point, why are you incapable of simply giving the enum an
>>>     underlying type and thus making the question moot?
>>     Because the enum is declared in a 3pp library in C++98? or a
>>     common part that is shared by an application using C++98 and
>>     another using C++2x?
>>
>>
>> So the enum is in legacy code (one way or another). And the enum is=20
>> not being used as a true enumeration, but as a general value that may=20
>> or may not resolve to one of the enumerators. And you're converting=20
>> an arbitrary integer into that enumeration.
>>
>> Ultimately, I'm not sure that this confluence of issues comes up=20
>> often enough that we need a mechanism to check for it.
>>
>>>         This is not a use case I would write myself, but I've see it
>>>         a lot of times. When you use enums as flags of an bitset
>>>         enum class X { NONE=3D0, A=3D0x01, B=3D0x02, C=3D0x04, ALL 0x08=
};
>>>
>>>         The valid enumerators  don't correspond to the valid range,
>>>         that in this case is any value between 0 and 8.
>>>
>>>
>>>     But we can already answer that question. `X` is an `enum class`.
>>>     As such, it /always/ has a fixed underlying type. If you don't
>>>     specify one, then it shall be `int`, and therefore `X` can
>>>     legally assume any `int` value.
>>     From the text of the standard I send before, the range is not the
>>     range of int. Could you point me from where are you concluding this?
>>
>>
>> [dcl.enum]/5:
>>
>> >  For a scoped enumeration type, the underlying type is int if it is=20
>> not explicitly specified. In both of these cases, the underlying type=20
>> is said to be fixed.
>>
>> [dcl.enum]/8:
>>
>> > For an enumeration whose underlying type is fixed, the values of=20
>> the enumeration are the values of the underlying type.
>>
>> So the range of a scoped enumeration is /always/ the range of its=20
>> underlying type. Therefore the situation you're talking about can=20
>> only come about by using non-scoped, non-fixed enums.
> Thanks this references clarifies the range of valid values. SO the=20
> difference isn't between explicit or not , but between enum and scoped=20
> enum.
>
I was wondering if the rationale for the different behavior has=20
something to be with the fact that scoped enums can be forward declared.

enum class X;

X f(int i) {
     return X(i); // we are able to construct them independently of the
}

int main()
{
    X x =3D f(1);
    return static_cast<int>(x);
}

http://melpon.org/wandbox/permlink/LiGCmCNwENTskhtv

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/c8da1219-1d83-4812-f099-3b270d179ab7%40wanadoo.f=
r.

--------------F966C3143431216CDC3D0344
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta content=3D"text/html; charset=3Dutf-8" http-equiv=3D"Content-Type=
">
  </head>
  <body bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div class=3D"moz-cite-prefix">Le 13/01/2017 =C3=A0 01:09, Vicente J. B=
otet
      Escriba a =C3=A9crit=C2=A0:<br>
    </div>
    <blockquote
      cite=3D"mid:b2c10414-ec5d-9bc3-dd7e-48bff6f1fa5e@wanadoo.fr"
      type=3D"cite">
      <meta content=3D"text/html; charset=3Dutf-8" http-equiv=3D"Content-Ty=
pe">
      <div class=3D"moz-cite-prefix">Le 12/01/2017 =C3=A0 17:07, Nicol Bola=
s a
        =C3=A9crit=C2=A0:<br>
      </div>
      <blockquote
        cite=3D"mid:d4065d2b-f8b7-4b55-ba43-65895c14e0bb@isocpp.org"
        type=3D"cite">
        <div dir=3D"ltr"><br>
          <br>
          On Thursday, January 12, 2017 at 2:28:00 AM UTC-5, Vicente J.
          Botet Escriba wrote:
          <blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left:
            0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
            <div bgcolor=3D"#FFFFFF" text=3D"#000000">
              <div>Le 11/01/2017 =C3=A0 22:21, Nicol Bolas a =C3=A9crit=C2=
=A0:<br>
              </div>
              <blockquote type=3D"cite">
                <div dir=3D"ltr">On Wednesday, January 11, 2017 at 1:52:11
                  PM UTC-5, Vicente J. Botet Escriba wrote:
                  <blockquote class=3D"gmail_quote"
                    style=3D"margin:0;margin-left:0.8ex;border-left:1px
                    #ccc solid;padding-left:1ex">
                    <div bgcolor=3D"#FFFFFF" text=3D"#000000">
                      <div>Le 11/01/2017 =C3=A0 00:06, Nicol Bolas a =C3=A9=
crit=C2=A0:<br>
                      </div>
                      <blockquote type=3D"cite">
                        <div dir=3D"ltr">On Tuesday, January 10, 2017 at
                          5:26:11 PM UTC-5, Vicente J. Botet Escriba
                          wrote: </div>
                      </blockquote>
                      <blockquote type=3D"cite">
                        <div dir=3D"ltr">
                          <div><br>
                            The question I have to ask is this: if you
                            have an integer, why do you need to know if
                            it will fit within the range of an enum with
                            an implied underlying type?</div>
                        </div>
                      </blockquote>
                      Because initializing the enum with an integer out
                      of range is UB.<br>
                    </div>
                  </blockquote>
                  <div><br>
                    You're misunderstanding my question.<br>
                    <br>
                    The situation you describe is one where:<br>
                    <br>
                    1. You have an integer of arbitrary origin.<br>
                    <br>
                    2. You want to convert it to an enum type.<br>
                    <br>
                    3. That integer <i>does not match</i> one of the
                    enumerators in that type.<br>
                    <br>
                    4. The enum type does not have a fixed underlying
                    type.<br>
                    <br>
                  </div>
                </div>
              </blockquote>
              You have described very well the context.<br>
              <blockquote type=3D"cite">
                <div dir=3D"ltr">
                  <div>What goal are you trying to achieve with all
                    this? Or more to the point, why are you incapable of
                    simply giving the enum an underlying type and thus
                    making the question moot?<br>
                  </div>
                </div>
              </blockquote>
              Because the enum is declared in a 3pp library in C++98? or
              a common part that is shared by an application using C++98
              and another using C++2x?<br>
            </div>
          </blockquote>
          <div><br>
            So the enum is in legacy code (one way or another). And the
            enum is not being used as a true enumeration, but as a
            general value that may or may not resolve to one of the
            enumerators. And you're converting an arbitrary integer into
            that enumeration.<br>
            <br>
            Ultimately, I'm not sure that this confluence of issues
            comes up often enough that we need a mechanism to check for
            it.<br>
          </div>
          <blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left:
            0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
            <div bgcolor=3D"#FFFFFF" text=3D"#000000">
              <blockquote type=3D"cite">
                <div dir=3D"ltr">
                  <blockquote class=3D"gmail_quote"
                    style=3D"margin:0;margin-left:0.8ex;border-left:1px
                    #ccc solid;padding-left:1ex">
                    <div bgcolor=3D"#FFFFFF" text=3D"#000000">This is not a
                      use case I would write myself, but I've see it a
                      lot of times. When you use enums as flags of an
                      bitset<br>
                      enum class X { NONE=3D0, A=3D0x01, B=3D0x02, C=3D0x04=
, ALL
                      0x08};<br>
                      <br>
                      The valid enumerators=C2=A0 don't correspond to the
                      valid range, that in this case is any value
                      between 0 and 8.<br>
                    </div>
                  </blockquote>
                  <div><br>
                    But we can already answer that question. `X` is an
                    `enum class`. As such, it <i>always</i> has a fixed
                    underlying type. If you don't specify one, then it
                    shall be `int`, and therefore `X` can legally assume
                    any `int` value.<br>
                  </div>
                </div>
              </blockquote>
              From the text of the standard I send before, the range is
              not the range of int. Could you point me from where are
              you concluding this?<br>
            </div>
          </blockquote>
          <div><br>
            [dcl.enum]/5:<br>
            <br>
            &gt;=C2=A0 For a scoped enumeration type, the underlying type i=
s
            int if it is not explicitly specified. In both of these
            cases, the underlying type is said to be fixed.<br>
            <br>
            [dcl.enum]/8:<br>
            <br>
            &gt; For an enumeration whose underlying type is fixed, the
            values of the enumeration are the values of the underlying
            type.<br>
            <br>
            So the range of a scoped enumeration is <i>always</i> the
            range of its underlying type. Therefore the situation you're
            talking about can only come about by using non-scoped,
            non-fixed enums.<br>
          </div>
        </div>
      </blockquote>
      Thanks this references clarifies the range of valid values. SO the
      difference isn't between explicit or not , but between enum and
      scoped enum.<br>
      <br>
    </blockquote>
    I was wondering if the rationale for the different behavior has
    something to be with the fact that scoped enums can be forward
    declared.<br>
    <br>
    enum class X;<br>
    <br>
    X f(int i) {<br>
    =C2=A0=C2=A0=C2=A0 return X(i); // we are able to construct them indepe=
ndently of
    the <br>
    }<br>
    <br>
    int main() <br>
    {=C2=A0=C2=A0 <br>
    =C2=A0=C2=A0 X x =3D f(1); <br>
    =C2=A0=C2=A0 return static_cast&lt;int&gt;(x);<br>
    }<br>
    <br>
    <a class=3D"moz-txt-link-freetext" href=3D"http://melpon.org/wandbox/pe=
rmlink/LiGCmCNwENTskhtv">http://melpon.org/wandbox/permlink/LiGCmCNwENTskht=
v</a><br>
    <br>
    Vicente<br>
  </body>
</html>

<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/c8da1219-1d83-4812-f099-3b270d179ab7%=
40wanadoo.fr?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/c8da1219-1d83-4812-f099-3b270d179ab7=
%40wanadoo.fr</a>.<br />

--------------F966C3143431216CDC3D0344--

.
