220 29246 <8da39f5b-a120-4be6-b9fd-35b9e9a28889@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Tim <thaines.astro@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: explicit conversion from enum class to its
 underlying type
Date: Sat, 29 Oct 2016 16:55:09 -0700 (PDT)
Lines: 153
Approved: news@gmane.org
Message-ID: <8da39f5b-a120-4be6-b9fd-35b9e9a28889@isocpp.org>
References: <4afed494-4fc5-4b06-b3e7-255aa293068e@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1999_1551261694.1477785309285"
X-Trace: blaine.gmane.org 1477785326 22249 195.159.176.226 (29 Oct 2016 23:55:26 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sat, 29 Oct 2016 23:55:26 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBD2NX2O34YLRBXXN2TAAKGQEXRPPPZI@isocpp.org Sun Oct 30 01:55:22 2016
Return-path: <std-proposals+bncBD2NX2O34YLRBXXN2TAAKGQEXRPPPZI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-it0-f72.google.com ([209.85.214.72])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBD2NX2O34YLRBXXN2TAAKGQEXRPPPZI@isocpp.org>)
	id 1c0dSu-0003N3-3g
	for gclcip-std-proposals@m.gmane.org; Sun, 30 Oct 2016 01:55:08 +0200
Original-Received: by mail-it0-f72.google.com with SMTP id q75sf77766066itc.4
        for <gclcip-std-proposals@m.gmane.org>; Sat, 29 Oct 2016 16:55:11 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=m0UXj8WAYQER29duuqH0f7VwPokG7wU76eJvchCWypU=;
        b=D9Gg9ePidb5j1A7SkVizMr9fa6f2A7GRZ18hLzt1MQlMW3NWr4kNaGgB6O7j+K7WyE
         qttDUnr8Ep55psjZZ3f4zXGLq/fS2xZ2sa0MRRIsi4Vf75h+yfHZ3/ln/MtEz0BbdibZ
         VBep211d4mF7AQkbakWAs1K9CzRkJu0dRLx6TJxUk8i0dsMXFKvV2arXr/nDWCZW0RLo
         zXb2CzHOaF4F8y/Hfmaau3MUXrlb7o2F56fZ3+CtOxjzLfvL2sG4vqy5d1DIhYQhOjtn
         zO8IWqyLn7try85pXhjpr+S53aQPOyTAF+oIJS8Z+7am7JCqjcBXJAfgxIrpfHZEH6H2
         cbvg==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=m0UXj8WAYQER29duuqH0f7VwPokG7wU76eJvchCWypU=;
        b=H+9YzKUA+bOzkTZUh/FjpKPHObYEpQaOZAFZH2sGcCZauXzs8xR9hVH9WVZs5epDTv
         P19TUGCbTzF4OGT8s9+snz9jV6cL2xjveVxoVoYN4RHW6CIjWH2mxrKmGnkMeX4wmKVT
         rUtB++zQfSZ/g8eEdkQAFaRpnPk1n7uBNZGShnTZUsk5RRCPxfI2uTVeQWduVVN9KrSU
         8KFD5LWI0zbVjKl/220KFmQNpqweqQYbGoa850xHWSjX/cbhM0NFOI62KFIC3b+tPmwR
         5/cBgjK2riC8Ii0vYsaamLEdc1GghcDfyirWdhvm3tDrx/JGMeBSH1O90Ayf+Hen65aq
         jKFA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :subject:mime-version:x-original-sender:reply-to:precedence
         :mailing-list:list-id:x-spam-checked-in-group:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=m0UXj8WAYQER29duuqH0f7VwPokG7wU76eJvchCWypU=;
        b=PTUmala63IdT+KsS59BYeuOb+hwJiBh+pnXTPbHcLOw1akiImO6rvYa72/iLfxLiP+
         YlSD0gKRFoWcJZptLzvc/4C+QkeGkpUysP5RkkiXj/ZqXAAli3tW7hD8rWyj7oY+CG6E
         2x3Tixcys09foZbwlpu/9CtWijd8YbS2AqiD8UD2zNNIOfTuGkw6WuFdrrhtUY3IpvzN
         mZLYhhLfxclAodsz04dhzP184mpRvqq5v2k6tBVOas8fqyZZAbhIZclT7vscL/tmRGUp
         cdcpDtK6ckxKbU/gMtdQ5PZcRbI7ztL7xwNV8BedPGfXh6jOhl2H98IbiBTNiYlx2QMj
         WSfQ==
X-Gm-Message-State: ABUngveu+yqqKZk5RyLeuq2SfpDepKg7yjkGWmFuQFqXUJbwcRgRy2IRCHCT0b+c2Zt/6Q==
X-Received: by 10.36.125.69 with SMTP id b66mr2272764itc.37.1477785310503;
        Sat, 29 Oct 2016 16:55:10 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.36.51 with SMTP id p48ls9632856ota.22.gmail; Sat, 29 Oct
 2016 16:55:09 -0700 (PDT)
X-Received: by 10.157.15.143 with SMTP id d15mr2409354otd.2.1477785309841;
        Sat, 29 Oct 2016 16:55:09 -0700 (PDT)
In-Reply-To: <4afed494-4fc5-4b06-b3e7-255aa293068e@isocpp.org>
X-Original-Sender: thaines.astro@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:29246
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/29246>

------=_Part_1999_1551261694.1477785309285
Content-Type: multipart/alternative; 
	boundary="----=_Part_2000_1717817825.1477785309286"

------=_Part_2000_1717817825.1477785309286
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

We already have explicit conversions for scoped enums (they aren't pretty,=
=20
though)

#include <type_traits>

enum class SkipUnused : bool {No, Yes};

void process(SkipUnused skipUnused) {
if (static_cast<std::underlying_type<SkipUnused>::type>(skipUnused)){}
}


On Saturday, October 29, 2016 at 4:51:12 PM UTC-5, Andrzej Krzemie=C5=84ski=
=20
wrote:
>
> Hi All,
>
> One of the nice safety features of enum classes is that they cannot be=20
> implicitly converted to their underlying type. However, do you think we=
=20
> would lose this safety feature if we allowed an explicit conversion to th=
e=20
> underlying type?
>
> I do not know of any practical use of explicit conversion operators excep=
t=20
> for the contextual conversion to bool. And it is only this conversion tha=
t=20
> I am interested in.
>
> It would be beneficial to have a unique Boolean type that would not be=20
> convertible to another unique Boolean type. One solution is to use an enu=
m=20
> class with `bool` as underlying type:
>
> enum class SkipUnused : bool { No, Yes };
> enum class SkipNeative : bool { No, Yes };
>
> void process(SkipUnused skipUnused)
> {
>   if (skipUnused) // doesn't compile
>     {}
> }
>
> Unfortunately, such enum-based type does not work with boolean=20
> expressions. It would we had an explicit conversion.
>
> Do you think, it would be acceptable to add such explicit conversion to=
=20
> the underlying type, for enum classes?
>
> Regards,
> &rzej;
>

--=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/8da39f5b-a120-4be6-b9fd-35b9e9a28889%40isocpp.or=
g.

------=_Part_2000_1717817825.1477785309286
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">We already have explicit conversions for scoped enums (the=
y aren&#39;t pretty, though)<div><br></div><div><div>#include &lt;type_trai=
ts&gt;</div><div><br></div><div>enum class SkipUnused : bool {No, Yes};<br>=
</div><div><br></div><div>void process(SkipUnused skipUnused) {</div><div><=
span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>if (static_=
cast&lt;std::underlying_type&lt;SkipUnused&gt;::type&gt;(skipUnused)){}</di=
v><div>}</div><div><br></div><div><br></div>On Saturday, October 29, 2016 a=
t 4:51:12 PM UTC-5, Andrzej Krzemie=C5=84ski wrote:<blockquote class=3D"gma=
il_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid=
;padding-left: 1ex;"><div dir=3D"ltr">Hi All,<br><br>One of the nice safety=
 features of enum classes is that they cannot be implicitly converted to th=
eir underlying type. However, do you think we would lose this safety featur=
e if we allowed an explicit conversion to the underlying type?<br><br>I do =
not know of any practical use of explicit conversion operators except for t=
he contextual conversion to bool. And it is only this conversion that I am =
interested in.<br><br>It would be beneficial to have a unique Boolean type =
that would not be convertible to another unique Boolean type. One solution =
is to use an enum class with `bool` as underlying type:<br><br><div style=
=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-=
style:solid;border-width:1px"><code><div><span style=3D"color:#008">enum</s=
pan><span style=3D"color:#000"> </span><span style=3D"color:#008">class</sp=
an><span style=3D"color:#000"> </span><span style=3D"color:#606">SkipUnused=
</span><span style=3D"color:#000"> </span><span style=3D"color:#660">:</spa=
n><span style=3D"color:#000"> </span><span style=3D"color:#008">bool</span>=
<span style=3D"color:#000"> </span><span style=3D"color:#660">{</span><span=
 style=3D"color:#000"> </span><span style=3D"color:#606">No</span><span sty=
le=3D"color:#660">,</span><span style=3D"color:#000"> </span><span style=3D=
"color:#606">Yes</span><span style=3D"color:#000"> </span><span style=3D"co=
lor:#660">};</span><span style=3D"color:#000"><br></span><span style=3D"col=
or:#008">enum</span><span style=3D"color:#000"> </span><span style=3D"color=
:#008">class</span><span style=3D"color:#000"> </span><span style=3D"color:=
#606">SkipNeative</span><span style=3D"color:#000"> </span><span style=3D"c=
olor:#660">:</span><span style=3D"color:#000"> </span><span style=3D"color:=
#008">bool</span><span style=3D"color:#000"> </span><span style=3D"color:#6=
60">{</span><span style=3D"color:#000"> </span><span style=3D"color:#606">N=
o</span><span style=3D"color:#660">,</span><span style=3D"color:#000"> </sp=
an><span style=3D"color:#606">Yes</span><span style=3D"color:#000"> </span>=
<span style=3D"color:#660">};</span><span style=3D"color:#000"><br><br></sp=
an><span style=3D"color:#008">void</span><span style=3D"color:#000"> proces=
s</span><span style=3D"color:#660">(</span><span style=3D"color:#606">SkipU=
nused</span><span style=3D"color:#000"> skipUnused</span><span style=3D"col=
or:#660">)</span><span style=3D"color:#000"><br></span><span style=3D"color=
:#660">{</span><span style=3D"color:#000"><br>=C2=A0 </span><span style=3D"=
color:#008">if</span><span style=3D"color:#000"> </span><span style=3D"colo=
r:#660">(</span><span style=3D"color:#000">skipUnused</span><span style=3D"=
color:#660">)</span><span style=3D"color:#000"> </span><span style=3D"color=
:#800">// doesn&#39;t compile</span><span style=3D"color:#000"><br>=C2=A0 =
=C2=A0 </span><span style=3D"color:#660">{}</span><span style=3D"color:#000=
"><br></span><span style=3D"color:#660">}</span></div></code></div><br>Unfo=
rtunately, such enum-based type does not work with boolean expressions. It =
would we had an explicit conversion.<br><br>Do you think, it would be accep=
table to add such explicit conversion to the underlying type, for enum clas=
ses?<br><br>Regards,<br>&amp;rzej;<br></div></blockquote></div></div>

<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/8da39f5b-a120-4be6-b9fd-35b9e9a28889%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/8da39f5b-a120-4be6-b9fd-35b9e9a28889=
%40isocpp.org</a>.<br />

------=_Part_2000_1717817825.1477785309286--

------=_Part_1999_1551261694.1477785309285--

.
