220 33458 <b7df0078-2395-4def-bd73-fd708083969d@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Nicol Bolas <jmckesson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Proposal: underlying_cast
Date: Sun, 30 Jul 2017 17:43:29 -0700 (PDT)
Lines: 134
Approved: news@gmane.org
Message-ID: <b7df0078-2395-4def-bd73-fd708083969d@isocpp.org>
References: <6a2c32ed-99b5-4cc5-86a5-ee3cc07567a3@isocpp.org>
 <20170731003821.GA7900@noemi.bahnhof.se>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_3649_911313278.1501461809434"
X-Trace: blaine.gmane.org 1501461815 30546 195.159.176.226 (31 Jul 2017 00:43:35 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 31 Jul 2017 00:43:35 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBMX27HFQKGQETODYYDQ@isocpp.org Mon Jul 31 02:43:27 2017
Return-path: <std-proposals+bncBCEKFTV6ZUMBBMX27HFQKGQETODYYDQ@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+bncBCEKFTV6ZUMBBMX27HFQKGQETODYYDQ@isocpp.org>)
	id 1dbynu-0007Pq-FH
	for gclcip-std-proposals@m.gmane.org; Mon, 31 Jul 2017 02:43:26 +0200
Original-Received: by mail-it0-f72.google.com with SMTP id 7sf250542098ita.0
        for <gclcip-std-proposals@m.gmane.org>; Sun, 30 Jul 2017 17:43:31 -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
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=CdRRp0a02jDsBEf8f4leTVnOp44+rf/OPquXT/XCVI8=;
        b=Ne50wR1DcUtua5Yn1Q/w6v7EtlCLwG1y+t+mlMl/Eho0h4dHaPtEAzxtx/vcc2IBTS
         WxnDbb07qZo77YuMtlqA5Y8XMqQGEQSGURy0qj9gTPxS92S13lqnT4epAxBXoyIMGg2d
         4Ld6xkAWHeo3X/E/X+vKI+xoeU95Ux6hyCroFPzmuiWw/No+mqZaw9RkM3tj3tV8d5M2
         VuNPkntPE+b3HP1LWT58M1R3svdaTyd4Z0K3xqBmdFD+Cwuv5fQi6eMsx11/RWYdmJId
         8sdwTqsAZIIoBV7FU0sYeF2bK9rFvpmTNfedqTxiE589g/al4/vA01AdGZkpysvLxvvg
         5/dg==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=CdRRp0a02jDsBEf8f4leTVnOp44+rf/OPquXT/XCVI8=;
        b=cE/Y+/t8As0CxfPYjtmegDhFywAiKABoS804g02slO9T5QaqvzhXXPD4ps0pWVpBGX
         59WbGYyT2XS7dju/djl/97NHnGWbwlUq4/M/dgMxLlkatSQwLnrhQEFRlFpzAJjzuvvM
         9Sl0foOcnY+FEf5Vke+/8Vu5qLZrrR2vKVkblflgRxx0F45ZJSZB4WbMQibPVM8X4HBZ
         DvvpYBZQ2fjFQrngt8gglSCNPxhx1mHxHkcLbgqNjSXXuFMJ7Vsoae3RVT2rFEiZ7oBw
         OtUY4V60UzUuhI7iavPMswJ5ehYryTdSalEvxfqGgRr5CkoffsZOkmaX0eV49qTgzvf/
         SHaw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        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=CdRRp0a02jDsBEf8f4leTVnOp44+rf/OPquXT/XCVI8=;
        b=TmERPva1HQwTkmwppn+kJGOiqIeC10bBEJBKkPkYqmGP1YlYp+uODaMmVntFFr5R2k
         wuhV073q2ngy94Yatu5eaiAFlVbWDqT5141AHtekv+W/bCCLYKCRw23KIgQyTiM15guh
         j/ZD64t3Kw+0WbdQgcgu8i7yl1BsTO34cgpBX0dHGS/nY8u9MTTdzYV4n1UsbOkLQSnv
         Sf7WvBEhIbY1x3aqdHs343Ni92ceO3BzfzWOgkpE91p16hI7p6s+TI5/HsgDTx97nk1I
         8kAVu0hJNlbltt/jg4ybOsre8+S3yEx0sdKBGv4hJABb+GcYOXus7IxowxA7U6Ow5SUL
         UZ4g==
X-Gm-Message-State: AIVw112Y+84WM1hFdBCBniOii/F69A4oxrWs5CohcXqUq1vhwtuhohrO
	U547D/hoIzI8CUFw
X-Received: by 10.107.18.225 with SMTP id 94mr10508510ios.102.1501461811271;
        Sun, 30 Jul 2017 17:43:31 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.36.23.207 with SMTP id 198ls7340820ith.18.gmail; Sun, 30 Jul
 2017 17:43:29 -0700 (PDT)
X-Received: by 10.31.179.145 with SMTP id c139mr76734vkf.24.1501461809896;
        Sun, 30 Jul 2017 17:43:29 -0700 (PDT)
In-Reply-To: <20170731003821.GA7900@noemi.bahnhof.se>
X-Original-Sender: jmckesson@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:33458
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/33458>

------=_Part_3649_911313278.1501461809434
Content-Type: multipart/alternative; 
	boundary="----=_Part_3650_846813795.1501461809434"

------=_Part_3650_846813795.1501461809434
Content-Type: text/plain; charset="UTF-8"



On Sunday, July 30, 2017 at 8:38:28 PM UTC-4, Magnus Fromreide wrote:
>
> On Sun, Jul 30, 2017 at 04:50:41AM -0700, james.a...@gmail.com 
> <javascript:> wrote: 
> > C++ allows enums to have an underlying type specified. When serialising 
> > enum values it's important to use the correct type. 
> > This proposal is to add a utility function that casts an enum type to 
> its 
> > underlying value. This would make it easier to handle 
> > enum types in function overloads for serialisation. For example: 
> > 
> > template <typename T> 
> > std::ostream& write(std::ostream& os, const T& v) 
> > { 
> >     os << std::underlying_cast(v); 
> >     return os; 
> > } 
> > 
> > 
> > This function would, in the case of enums cast the value to the 
> underlying 
> > type, and in the case of any other type it would do nothing. 
> > 
> > One possible implementation: 
> > 
> > template <typename E> 
> > using UnderlyingType = typename std::underlying_type<E>::type; 
>
> What about recursive enums? 
>
> enum foo : int { value1 = 4096 }; 
> enum bar : foo { value2 = 4095 }; 
>
> What does std::underlying_type<bar>::type resolve to? 
>

Nothing, because `bar` is ill-formed:

> The type-specifier-seq of an enum-base shall name an integral type; any 
cv-qualification is ignored

Enumeration types are not "integral type"s.
 

>
> /MF 
>

-- 
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 email 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/b7df0078-2395-4def-bd73-fd708083969d%40isocpp.org.

------=_Part_3650_846813795.1501461809434
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, July 30, 2017 at 8:38:28 PM UTC-4, Magn=
us Fromreide wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;mar=
gin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Sun, Jul=
 30, 2017 at 04:50:41AM -0700, <a href=3D"javascript:" target=3D"_blank" gd=
f-obfuscated-mailto=3D"HUFVxtHFAQAJ" rel=3D"nofollow" onmousedown=3D"this.h=
ref=3D&#39;javascript:&#39;;return true;" onclick=3D"this.href=3D&#39;javas=
cript:&#39;;return true;">james.a...@gmail.com</a> wrote:
<br>&gt; C++ allows enums to have an underlying type specified. When serial=
ising=20
<br>&gt; enum values it&#39;s important to use the correct type.
<br>&gt; This proposal is to add a utility function that casts an enum type=
 to its=20
<br>&gt; underlying value. This would make it easier to handle
<br>&gt; enum types in function overloads for serialisation. For example:
<br>&gt;=20
<br>&gt; template &lt;typename T&gt;
<br>&gt; std::ostream&amp; write(std::ostream&amp; os, const T&amp; v)
<br>&gt; {
<br>&gt; =C2=A0 =C2=A0 os &lt;&lt; std::underlying_cast(v);
<br>&gt; =C2=A0 =C2=A0 return os;
<br>&gt; }
<br>&gt;=20
<br>&gt;=20
<br>&gt; This function would, in the case of enums cast the value to the un=
derlying=20
<br>&gt; type, and in the case of any other type it would do nothing.
<br>&gt;=20
<br>&gt; One possible implementation:
<br>&gt;=20
<br>&gt; template &lt;typename E&gt;
<br>&gt; using UnderlyingType =3D typename std::underlying_type&lt;E&gt;::t=
ype;
<br>
<br>What about recursive enums?
<br>
<br>enum foo : int { value1 =3D 4096 };
<br>enum bar : foo { value2 =3D 4095 };
<br>
<br>What does std::underlying_type&lt;bar&gt;::<wbr>type resolve to?
<br></blockquote><div><br>Nothing, because `bar` is ill-formed:<br><br>&gt;=
 The type-specifier-seq of an enum-base shall name an integral type; any cv=
-qualification is ignored<br><br>Enumeration types are not &quot;integral t=
ype&quot;s.<br>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margi=
n: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
<br>/MF
<br></blockquote></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/b7df0078-2395-4def-bd73-fd708083969d%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/b7df0078-2395-4def-bd73-fd708083969d=
%40isocpp.org</a>.<br />

------=_Part_3650_846813795.1501461809434--

------=_Part_3649_911313278.1501461809434--

.
