220 30467 <e779f1ca-8f05-7eda-72c8-89a9a5215c62@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: Tue, 10 Jan 2017 19:49:01 +0100
Lines: 320
Approved: news@gmane.org
Message-ID: <e779f1ca-8f05-7eda-72c8-89a9a5215c62@wanadoo.fr>
References: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
 <57258435-5d82-48e7-8ed7-3c38682418fb@isocpp.org>
 <a35d9f20-9e4f-4789-b7e5-5cb08f99e6bd@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="------------5BE1BAD55E64866D1B74C1D4"
X-Trace: blaine.gmane.org 1484074161 21136 195.159.176.226 (10 Jan 2017 18:49:21 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 10 Jan 2017 18:49:21 +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+bncBDH67CONY4PBBH6Z2TBQKGQEOTI72DQ@isocpp.org Tue Jan 10 19:49:11 2017
Return-path: <std-proposals+bncBDH67CONY4PBBH6Z2TBQKGQEOTI72DQ@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+bncBDH67CONY4PBBH6Z2TBQKGQEOTI72DQ@isocpp.org>)
	id 1cR1Tg-0003o7-3Q
	for gclcip-std-proposals@m.gmane.org; Tue, 10 Jan 2017 19:49:00 +0100
Original-Received: by mail-lf0-f72.google.com with SMTP id b200sf58369105lfe.0
        for <gclcip-std-proposals@m.gmane.org>; Tue, 10 Jan 2017 10:49:05 -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=eECebt+9UDHOugek2uUih+gQLcgqFQXqGCh59N4wFXA=;
        b=ND3N42sI8v8cHr0ezx0f0tHBgyKzvLfVkjLb5GZ/f2GSIeS/ApOtNq+/zc9fMLngjV
         a+246teU+TDUoL+hjWZitthS+cRzPyuDGK52ljrFS5fSiBLUOZKI74w8G0HSUBrvuG16
         m8BsCcCkPzYUVssTe83jsY4dTlltUohJWxCkQihWaZYjU+YMmwcjsb2wGvvbgdHRMSt5
         8Pw5QzAUxsQaQQDnq304IDY6CnDYnxRdKyXadkkvkODPatdzrXJpa220R4QzwuzyzIai
         hFXCypAIAP/eumVm3H+piVkPJP7HnqycMIlQyKGktuX4P/oRRFv0TmvFyNPClng8C4qj
         Zubw==
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=eECebt+9UDHOugek2uUih+gQLcgqFQXqGCh59N4wFXA=;
        b=FSJ81o5vA8/9LG7TcSz+6tG4gym02ay22HMxa8YI1O9AqAuK6mBkwPk1KdpYlHnxsq
         T3ZjnsPis0yu2a1uDFIf75D7jrmfLMqxV+qKM312OpIwnUELfdDuj7bgwevzy0x6xeH6
         l2/Hj4H71I6VQ84yDof5nmtFpq4hSRVvOFflldAn8UCNSK57SIfiAVDFuAU+2sxMjpbc
         Esh5n7mmUHggA+RGNqOXKZ9mOcUSkVLKuEE7EA6yfNEMtgDKDJWyey7faecs72MnwYo/
         QEX1COFpWNkDtc0+B/RD5ZApUtETwKEjWcJ3q2e2tCuqKYLC9oBHceTq+l6dEAHNSoUN
         6K1A==
X-Gm-Message-State: AIkVDXKzFh+6vtcHqoYlQVyq4za701SCcul4FyzCCSEswq6m4WogjNflHHIOaApRhqnhfw==
X-Received: by 10.25.26.2 with SMTP id a2mr397621lfa.13.1484074144902;
        Tue, 10 Jan 2017 10:49:04 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.37.4 with SMTP id l4ls1694456wml.19.canary-gmail; Tue, 10
 Jan 2017 10:49:03 -0800 (PST)
X-Received: by 10.194.235.226 with SMTP id up2mr2702826wjc.27.1484074143446;
        Tue, 10 Jan 2017 10:49:03 -0800 (PST)
Original-Received: from smtp.smtpout.orange.fr (smtp10.smtpout.orange.fr. [80.12.242.132])
        by mx.google.com with ESMTPS id 127si13738258wmv.35.2017.01.10.10.49.03
        for <std-proposals@isocpp.org>
        (version=TLS1 cipher=AES128-SHA bits=128/128);
        Tue, 10 Jan 2017 10:49:03 -0800 (PST)
Received-SPF: neutral (google.com: 80.12.242.132 is neither permitted nor denied by best guess record for domain of vicente.botet@wanadoo.fr) client-ip=80.12.242.132;
Original-Received: from imac-de-vicente-botet-escriba.home ([2.11.64.156])
	by mwinf5d20 with ME
	id Wip21u0093NGJZe03ip2Bu; Tue, 10 Jan 2017 19:49:03 +0100
X-ME-Helo: imac-de-vicente-botet-escriba.home
X-ME-Auth: dmljZW50ZS5ib3RldEB3YW5hZG9vLmZy
X-ME-Date: Tue, 10 Jan 2017 19:49:03 +0100
X-ME-IP: 2.11.64.156
In-Reply-To: <a35d9f20-9e4f-4789-b7e5-5cb08f99e6bd@isocpp.org>
X-Original-Sender: vicente.botet@wanadoo.fr
X-Original-Authentication-Results: mx.google.com;       spf=neutral
 (google.com: 80.12.242.132 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:30467
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30467>

This is a multi-part message in MIME format.
--------------5BE1BAD55E64866D1B74C1D4
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 10/01/2017 =C3=A0 16:31, m.cencora@gmail.com a =C3=A9crit :
>
> W dniu wtorek, 10 stycznia 2017 13:49:00 UTC+1 u=C5=BCytkownik=20
> gmis...@gmail.com napisa=C5=82:
>
>
>
>     On Tuesday, January 3, 2017 at 10:56:05 PM UTC+13,
>     m.ce...@gmail.com wrote:
>
>         Hi,
>
>
> I'll try to create a first draft after I get some more comments w.r.t=20
> to exceptions.
> For sure baseline API must be exception-free.
> Do we really want to additionally support API that throws on error?
> I'm rather inclined to leave it out - similarly as in to_chars/from_chars=
..
>
>
>     I hope this post assists you with your proposal. My suggestions
>     are for your proposal to look something like this:
>
>     In <utility> I think add:
>
>     bool template<typename E, typename V> is_enum( V ev )
>     {
>      // compiler magic:
>      // Calling this routine causes the compiler to generate or a call
>     a routine
>      // that returns true if the value ev matches one of the enum E's
>      // values. otherwise false.
>     }
>
>
> 'is_enum' name is already used in type_traits header.
> Also is_enum seems redundant to me, since to_enum already reports
> if convertion succeeded instead of:
> |
> if(is_enum<MyEnum>(139))
> |
>
> you could write:
> |
> if(to_enum<MyEnum>(139).is_valid)
> |
>
> or even:
> |
> if(to_enum<MyEnum>(139))
> |
> if we add explicit bool convertion operator for to_enum_result.
>
>
I believe we need two checking functions:
* is_enumerator : checks if the explicit conversion from the integer is=20
one of the explicit enumerators
* is_in_enum_range: checks if the integer is in the range of valid=20
values. This is the precondition of the static_cast.

IIUC when the underlying type is explicit, the range of values are the=20
range of the underlying type. However when the underlying type is=20
implicit the range goes from the min to the max of the values of the=20
enumerators.

Until we don't have an enum that restrict the validity of the values to=20
his explicit enumerators, the range check seems to be useful at least to=20
avoid UB.

It is easy to build then whatever we want on top of these checks.
to_enum: throw exception
to_enum_or
try_to_enum: return optional or whatever is more appropriated.

Having good names for both check variants will surely take some time.

Note that we don't know yet if expected<T,E> will throw bad_expected or E.

I believe we can start by a proposal that includes only the checks and=20
surface the possible usage.

Vicente

P.S. Both checks can be built on top of the static reflection interface.

--=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/e779f1ca-8f05-7eda-72c8-89a9a5215c62%40wanadoo.f=
r.

--------------5BE1BAD55E64866D1B74C1D4
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 10/01/2017 =C3=A0 16:31,
      <a class=3D"moz-txt-link-abbreviated" href=3D"mailto:m.cencora@gmail.=
com">m.cencora@gmail.com</a> a =C3=A9crit=C2=A0:<br>
    </div>
    <blockquote
      cite=3D"mid:a35d9f20-9e4f-4789-b7e5-5cb08f99e6bd@isocpp.org"
      type=3D"cite">
      <div dir=3D"ltr"><br>
        W dniu wtorek, 10 stycznia 2017 13:49:00 UTC+1 u=C5=BCytkownik
        <a class=3D"moz-txt-link-abbreviated" href=3D"mailto:gmis...@gmail.=
com">gmis...@gmail.com</a> napisa=C5=82:
        <blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left:
          0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
          <div dir=3D"ltr"><br>
            <br>
            On Tuesday, January 3, 2017 at 10:56:05 PM UTC+13, <a
              moz-do-not-send=3D"true">m.ce...@gmail.com</a> wrote:
            <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px
0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width=
:1px;border-left-style:solid">
              <div dir=3D"ltr">Hi,
                <div><br>
                </div>
              </div>
            </blockquote>
          </div>
        </blockquote>
        <br>
        <div>I'll try to create a first draft after I get some more
          comments w.r.t to exceptions.</div>
        <div>For sure baseline API must be exception-free.</div>
        <div>Do we really want to additionally support API that throws
          on error?</div>
        <div>I'm rather inclined to leave it out - similarly as in
          to_chars/from_chars.</div>
        <div>=C2=A0</div>
        <blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left:
          0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
          <div dir=3D"ltr">
            <div><br>
            </div>
            <div>I hope this post assists you with your proposal.=C2=A0My
              suggestions are for your proposal to look something like
              this:</div>
            <div><br>
            </div>
            <div>In &lt;utility&gt; I think add:</div>
            <div><br>
            </div>
            <div>bool template&lt;typename E, typename V&gt; is_enum( V
              ev )</div>
            <div>{</div>
            <div>=C2=A0// compiler=C2=A0magic:</div>
            <div>=C2=A0// Calling this routine causes the compiler to
              generate or a call a routine</div>
            <div>=C2=A0// that returns true if the value ev matches one of
              the enum E's</div>
            <div>=C2=A0// values. otherwise false.</div>
            <div>}</div>
          </div>
        </blockquote>
        <div><br>
        </div>
        <div>'is_enum' name is already used in type_traits header.<br>
        </div>
        <div>Also is_enum seems redundant to me, since to_enum already
          reports</div>
        <div>if convertion succeeded instead of:</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"><span style=3D"color: #008;"
                  class=3D"styled-by-prettify">if</span><span
                  style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span
                  style=3D"color: #000;" class=3D"styled-by-prettify">is_en=
um</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">&lt;<=
/span><span
                  style=3D"color: #606;" class=3D"styled-by-prettify">MyEnu=
m</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">&gt;(=
</span><span
                  style=3D"color: #066;" class=3D"styled-by-prettify">139</=
span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">))</s=
pan></div>
            </code></div>
          <br>
        </div>
        <div>you could write:</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"><span style=3D"color: #008;"
                  class=3D"styled-by-prettify">if</span><span
                  style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span
                  style=3D"color: #000;" class=3D"styled-by-prettify">to_en=
um</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">&lt;<=
/span><span
                  style=3D"color: #606;" class=3D"styled-by-prettify">MyEnu=
m</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">&gt;(=
</span><span
                  style=3D"color: #066;" class=3D"styled-by-prettify">139</=
span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">).</s=
pan><span
                  style=3D"color: #000;" class=3D"styled-by-prettify">is_va=
lid</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">)</sp=
an></div>
            </code></div>
          <br>
        </div>
        <div>or even:</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"><span style=3D"color: #008;"
                  class=3D"styled-by-prettify">if</span><span
                  style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span
                  style=3D"color: #000;" class=3D"styled-by-prettify">to_en=
um</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">&lt;<=
/span><span
                  style=3D"color: #606;" class=3D"styled-by-prettify">MyEnu=
m</span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">&gt;(=
</span><span
                  style=3D"color: #066;" class=3D"styled-by-prettify">139</=
span><span
                  style=3D"color: #660;" class=3D"styled-by-prettify">))</s=
pan></div>
            </code></div>
          if we add explicit bool convertion operator for
          to_enum_result.</div>
        <div><br>
        </div>
        <div><br>
        </div>
      </div>
    </blockquote>
    I believe we need two checking functions:<br>
    * is_enumerator : checks if the explicit conversion from the integer
    is one of the explicit enumerators<br>
    * is_in_enum_range: checks if the integer is in the range of valid
    values. This is the precondition of the static_cast.<br>
    <br>
    IIUC when the underlying type is explicit, the range of values are
    the range of the underlying type. However when the underlying type
    is implicit the range goes from the min to the max of the values of
    the enumerators.<br>
    <br>
    Until we don't have an enum that restrict the validity of the values
    to his explicit enumerators, the range check seems to be useful at
    least to avoid UB.<br>
    <br>
    It is easy to build then whatever we want on top of these checks.<br>
    to_enum: throw exception<br>
    to_enum_or<br>
    try_to_enum: return optional or whatever is more appropriated.<br>
    <br>
    Having good names for both check variants will surely take some
    time.<br>
    <br>
    Note that we don't know yet if expected&lt;T,E&gt; will throw
    bad_expected or E.<br>
    <br>
    I believe we can start by a proposal that includes only the checks
    and surface the possible usage.<br>
    <br>
    Vicente <br>
    <br>
    P.S. Both checks can be built on top of the static reflection
    interface.<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/e779f1ca-8f05-7eda-72c8-89a9a5215c62%=
40wanadoo.fr?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e779f1ca-8f05-7eda-72c8-89a9a5215c62=
%40wanadoo.fr</a>.<br />

--------------5BE1BAD55E64866D1B74C1D4--

.
