220 30188 <a19ed9c6-45c0-e3d7-e92a-06468bff63b7@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: enum_cast proposal
Date: Tue, 3 Jan 2017 19:25:18 +0100
Lines: 364
Approved: news@gmane.org
Message-ID: <a19ed9c6-45c0-e3d7-e92a-06468bff63b7@wanadoo.fr>
References: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="------------BB00DC1B62BA3E31F9F197A6"
X-Trace: blaine.gmane.org 1483467927 30353 195.159.176.226 (3 Jan 2017 18:25:27 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 3 Jan 2017 18:25:27 +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+bncBDH67CONY4PBBD6ZV7BQKGQEP2KV4WY@isocpp.org Tue Jan 03 19:25:22 2017
Return-path: <std-proposals+bncBDH67CONY4PBBD6ZV7BQKGQEP2KV4WY@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+bncBDH67CONY4PBBD6ZV7BQKGQEP2KV4WY@isocpp.org>)
	id 1cOTlr-0006bD-QO
	for gclcip-std-proposals@m.gmane.org; Tue, 03 Jan 2017 19:25:15 +0100
Original-Received: by mail-lf0-f69.google.com with SMTP id t196sf183396975lff.3
        for <gclcip-std-proposals@m.gmane.org>; Tue, 03 Jan 2017 10:25:20 -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: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=BrzDQesA63lSehyAvGViZuFppZY3Jx6DmrLYi8KoRxo=;
        b=vvIA8N0iUUfDKCF71yaapcC4skVc7dWaR7fNh/qVokm+hLh2Kg7IkUj6Tu4gCI6mB1
         5E43aWUtld21Y10rWh6UVd9eHjD1p7vnfo+hofaUQWEG6SI2UHN/DpDm64futyJby7Gz
         /4T9vtwl0QmGW9i/dsmFcsXTa8ESbR1T5DXX12owTMXel8N5khHtpw/aktFuEDSoLZYi
         e/zjk8qCjFogwkI8ZW+VCixZYzqS4trum6ukwcaFJ6syqKUumdSCDmV9wuKzNqZZQTSl
         MoyZmQVuqLeQ2d2fLLR9aY4T1lDBGB70unax3jfFwbMr6aZHMXk0v6BQeMPnXzosiBQ2
         UOcg==
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: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=BrzDQesA63lSehyAvGViZuFppZY3Jx6DmrLYi8KoRxo=;
        b=cElFNQMZpJTLidsS1QxKVcY+2IQ0NSRGnWdtr1C2ZZQgfx6r+e4LBaatjEkMJdSKiC
         0isk/lQhYwYMgH4vcGkLuXbygk17ltG/yNKBtCwfdVx5zqBwKXEBPISe2f9vadVRqynw
         l2PlVFd+Qo1EvwdAwfN0GYOAjhqY6zgsLPPeFOFBvCosNPSrOhhUIOdE000eTKwpoeKl
         KZe7uUUIC3n1YVJKv3u13hC57rUFlaoYwfe1Fl8M764ZS4TudyHYBpMFyuQRyUteE7fx
         R+f3ZoN5mIco+hAijGr7kBIvK8ls+JhiGfpfpX7pJR1C8vlRo9R+YORU1rtG4MCK/Hwm
         3rjA==
X-Gm-Message-State: AIkVDXL3OLmAtFFonG3mhtKAbJFv6OLWS7hN+eYndz9mB1d0opzcNMl6lm2kp+ET0En7cw==
X-Received: by 10.46.21.6 with SMTP id s6mr5918816ljd.23.1483467919966;
        Tue, 03 Jan 2017 10:25:19 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.166.66 with SMTP id p63ls5857719wme.5.gmail; Tue, 03 Jan
 2017 10:25:18 -0800 (PST)
X-Received: by 10.28.98.130 with SMTP id w124mr57341798wmb.125.1483467918897;
        Tue, 03 Jan 2017 10:25:18 -0800 (PST)
Original-Received: from smtp.smtpout.orange.fr (smtp06.smtpout.orange.fr. [80.12.242.128])
        by mx.google.com with ESMTPS id qo1si78058822wjc.294.2017.01.03.10.25.18
        for <std-proposals@isocpp.org>
        (version=TLS1 cipher=AES128-SHA bits=128/128);
        Tue, 03 Jan 2017 10:25:18 -0800 (PST)
Received-SPF: neutral (google.com: 80.12.242.128 is neither permitted nor denied by best guess record for domain of vicente.botet@wanadoo.fr) client-ip=80.12.242.128;
Original-Received: from imac-de-vicente-botet-escriba.home ([92.139.156.24])
	by mwinf5d12 with ME
	id TuRJ1u0030Xrduc03uRJQC; Tue, 03 Jan 2017 19:25:18 +0100
X-ME-Helo: imac-de-vicente-botet-escriba.home
X-ME-Auth: dmljZW50ZS5ib3RldEB3YW5hZG9vLmZy
X-ME-Date: Tue, 03 Jan 2017 19:25:18 +0100
X-ME-IP: 92.139.156.24
In-Reply-To: <efdf2357-00a6-408a-8772-12f99135a880@isocpp.org>
X-Original-Sender: vicente.botet@wanadoo.fr
X-Original-Authentication-Results: mx.google.com;       spf=neutral
 (google.com: 80.12.242.128 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:30188
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30188>

This is a multi-part message in MIME format.
--------------BB00DC1B62BA3E31F9F197A6
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 03/01/2017 =C3=A0 10:56, m.cencora@gmail.com a =C3=A9crit :
> Hi,
>
> I propose to add a enum_cast function that safely casts int value to a=20
> target enum type if int value represents a valid enumerator.
>
> |
> template<typenameEnum,typenameInt>
> optional<Enum>enum_cast(Intvalue);
>
> requires:is_enum<Enum>,is_integral<Int>
>
> enumcolor
> {
>    red =3D1,
>    green =3D200,
>    blue =3D3
> };
>
> static_assert(enum_cast<color>(200u)=3D=3Dcolor::green);
> static_assert(enum_cast<color>(3)=3D=3Dcolor::blue);
> static_assert(enum_cast<color>(4)=3D=3Dnullopt);
>
> |
>
> This should work for both scoped and unscoped enums.
> For opaque enums this would only check if value fits in underlying type.
>
> Why do we want this:
>  - validation of input data (e.g. coming from user or deserialization).
>  - other?
>
> I know this could be purely library extension if based on reflection,=20
> but since we don't know when we will get reflection,
> and implementation based on reflection may not be optimal,
> I think it should be implemented with compiler support (i.e. via=20
> __builtin_* intrinsic).
>
Hi, I believe this could be useful. Library implementers could be free=20
to use builtins or reflection once it is there.

Now, I believe that we need two kind of functions. One that is a cast=20
and that says just that we are casting. It would be the same as a=20
static_cast. Something like gsl::narrow_cast so that we express better=20
the intent. In addition we need the function you are proposing similar=20
to gsl::narrow.
The question is how the function reports errors. If we use exceptions, I=20
agree with Nicol that a specific exception would be better.

I will then propose
* enum_cast ~static_cast
* to_enum : throw exception if error
* try_to_enum : returns optional<Enum> or an interface based on=20
error_code (like from_chars). status_value and expected can be=20
considered also once adopted.
Do we expect several error conditions?

The fist is just a library solution. For the last two, we would need a=20
compiler builtin or reflection so that both can be easily and=20
efficiently implemented.

My 2cts.
Vicente

P.S. I don't expect this to be available before reflection, so an=20
implementation using the current reflection (see github) interface=20
should be welcome. This could be important to balance the compiler effort.

--=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/a19ed9c6-45c0-e3d7-e92a-06468bff63b7%40wanadoo.f=
r.

--------------BB00DC1B62BA3E31F9F197A6
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 03/01/2017 =C3=A0 10:56,
      <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:efdf2357-00a6-408a-8772-12f99135a880@isocpp.org"
      type=3D"cite">
      <div dir=3D"ltr">Hi,
        <div><br>
        </div>
        <div>I propose to add a enum_cast function that safely casts int
          value to a target enum type if int value represents a valid
          enumerator.</div>
        <div><br>
        </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">template</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #008;" class=3D"styled-by-prettify">typenam=
e</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Enum</s=
pan><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">typenam=
e</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Int</sp=
an><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</s=
pan><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                optional</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">&lt;</span><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Enum</s=
pan><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</s=
pan><span
                style=3D"color: #000;" class=3D"styled-by-prettify">
                enum_cast</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">(</span><span style=3D"color:
                #606;" class=3D"styled-by-prettify">Int</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> value<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">);</spa=
n><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                <br>
                requires</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">:</span><span style=3D"color:
                #000;" class=3D"styled-by-prettify"> is_enum</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><font
                color=3D"#000000"><span style=3D"color: #606;"
                  class=3D"styled-by-prettify">Enum</span></font><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;,</=
span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">
                is_integral</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">&lt;</span><span
                style=3D"color: #606;" class=3D"styled-by-prettify">Int</sp=
an><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</s=
pan><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                <br>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">enum</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> color<=
br>
              </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">{</span><span style=3D"color:
                #000;" class=3D"styled-by-prettify"><br>
                =C2=A0 =C2=A0red </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">=3D</span><span style=3D"color=
:
                #000;" class=3D"styled-by-prettify"> </span><span
                style=3D"color: #066;" class=3D"styled-by-prettify">1</span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                =C2=A0 =C2=A0green </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">=3D</span><span style=3D"color=
:
                #000;" class=3D"styled-by-prettify"> </span><span
                style=3D"color: #066;" class=3D"styled-by-prettify">200</sp=
an><span
                style=3D"color: #660;" class=3D"styled-by-prettify">,</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                =C2=A0 =C2=A0blue </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">=3D</span><span style=3D"color=
:
                #000;" class=3D"styled-by-prettify"> </span><span
                style=3D"color: #066;" class=3D"styled-by-prettify">3</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">};</span><span style=3D"color:
                #000;" class=3D"styled-by-prettify"><br>
                <br>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">static_assert</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify">enum_ca=
st</span><span
                style=3D"color: #080;" class=3D"styled-by-prettify">&lt;col=
or&gt;</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><font
                color=3D"#006666"><span style=3D"color: #066;"
                  class=3D"styled-by-prettify">200u</span></font><span
                style=3D"color: #660;" class=3D"styled-by-prettify">)</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">=3D=3D<=
/span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> color<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">::</spa=
n><font
                color=3D"#000000"><span style=3D"color: #000;"
                  class=3D"styled-by-prettify">green</span></font><span
                style=3D"color: #660;" class=3D"styled-by-prettify">);</spa=
n><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">static_assert</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify">enum_ca=
st</span><span
                style=3D"color: #080;" class=3D"styled-by-prettify">&lt;col=
or&gt;</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><span
                style=3D"color: #066;" class=3D"styled-by-prettify">3</span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">)</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">=3D=3D<=
/span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> color<=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">::</spa=
n><span
                style=3D"color: #000;" class=3D"styled-by-prettify">blue</s=
pan><span
                style=3D"color: #660;" class=3D"styled-by-prettify">);</spa=
n><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #008;"
                class=3D"styled-by-prettify">static_assert</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify">enum_ca=
st</span><span
                style=3D"color: #080;" class=3D"styled-by-prettify">&lt;col=
or&gt;</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><span
                style=3D"color: #066;" class=3D"styled-by-prettify">4</span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">)</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">=3D=3D<=
/span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> nullop=
t</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">);</spa=
n><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                <br>
              </span></div>
          </code></div>
        <div><br>
        </div>
        <div>This should work for both scoped and unscoped enums.</div>
        <div>For opaque enums this would only check if value fits in
          underlying type.<br>
        </div>
        <div><br>
        </div>
        <div>Why do we want this:<br>
        </div>
        <div>=C2=A0- validation of input data (e.g. coming from user or
          deserialization).</div>
        <div>=C2=A0- other?</div>
        <div><br>
        </div>
        <div>I know this could be purely library extension if based on
          reflection, but since we don't know when we will get
          reflection,</div>
        <div>and implementation based on reflection may not be optimal,</di=
v>
        <div>I think it should be implemented with compiler support
          (i.e. via __builtin_* intrinsic).</div>
        <div><br>
        </div>
      </div>
    </blockquote>
    Hi, I believe this could be useful. Library implementers could be
    free to use builtins or reflection once it is there.<br>
    <br>
    Now, I believe that we need two kind of functions. One that is a
    cast and that says just that we are casting. It would be the same as
    a static_cast. Something like gsl::narrow_cast so that we express
    better the intent. In addition we need the function you are
    proposing similar to gsl::narrow.<br>
    The question is how the function reports errors. If we use
    exceptions, I agree with Nicol that a specific exception would be
    better.<br>
    <br>
    I will then propose <br>
    * enum_cast ~static_cast<br>
    * to_enum : throw exception if error<br>
    * try_to_enum : returns optional&lt;Enum&gt; or an interface based
    on error_code (like from_chars). status_value and expected can be
    considered also once adopted.<br>
    Do we expect several error conditions?<br>
    <br>
    The fist is just a library solution. For the last two, we would need
    a compiler builtin or reflection so that both can be easily and
    efficiently implemented.<br>
    <br>
    My 2cts.<br>
    Vicente <br>
    <br>
    P.S. I don't expect this to be available before reflection, so an
    implementation using the current reflection (see github) interface
    should be welcome. This could be important to balance the compiler
    effort.<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/a19ed9c6-45c0-e3d7-e92a-06468bff63b7%=
40wanadoo.fr?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/a19ed9c6-45c0-e3d7-e92a-06468bff63b7=
%40wanadoo.fr</a>.<br />

--------------BB00DC1B62BA3E31F9F197A6--

.
