220 28008 <1f2b9290-581d-1fec-a98d-5efe4f5f8fc9@scylladb.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Avi Kivity <avi@scylladb.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: Make (strongly typed) enums castable to char
 const * const
Date: Sun, 28 Aug 2016 19:35:27 +0300
Organization: ScyllaDB
Lines: 246
Approved: news@gmane.org
Message-ID: <1f2b9290-581d-1fec-a98d-5efe4f5f8fc9@scylladb.com>
References: <1472132849.17129.9.camel@bsdforen.de>
 <CAFk2RUZiqeTBGir3fKehHzju6YDtbigdvThW=CsgkBSaExjrug@mail.gmail.com>
 <npn3iq$4ah$1@blaine.gmane.org>
 <CANh-dXmFXB6WY=uUt6Qh3PzTGek9e0Av-0h4TLnFzXSY6mAR+A@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="------------86C3882CFC8E2D41499A0DDB"
X-Trace: blaine.gmane.org 1472402139 31134 195.159.176.226 (28 Aug 2016 16:35:39 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sun, 28 Aug 2016 16:35:39 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.2.0
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDHJFOWD7YCRBUVFRS7AKGQEE3OBYZQ@isocpp.org Sun Aug 28 18:35:34 2016
Return-path: <std-proposals+bncBDHJFOWD7YCRBUVFRS7AKGQEE3OBYZQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lf0-f70.google.com ([209.85.215.70])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDHJFOWD7YCRBUVFRS7AKGQEE3OBYZQ@isocpp.org>)
	id 1be33S-0007EB-Ly
	for gclcip-std-proposals@m.gmane.org; Sun, 28 Aug 2016 18:35:30 +0200
Original-Received: by mail-lf0-f70.google.com with SMTP id p85sf83339584lfg.3
        for <gclcip-std-proposals@m.gmane.org>; Sun, 28 Aug 2016 09:35:32 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=subject:to:references:from:organization: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=MegUZaQqP+Z+mNgI0xq36cxm7P5v8q8y7DI1q/wy1Ck=;
        b=tvrMeHTFsur2M9Yt1XGObaIpVAevXuvz6RwTcm1++flp3J0UnhflSs/aT+udC3Lihw
         FGQ7aN7YVJJ57Yj6mmBt6OHH8IrFSJqswEZLYttW+itE2t9YRoqgG95Y5kUUyVNv05eu
         LdVfww/kk9W9/ZY5+7/Zp+oAYGfRk7vQeGaW38E+Ez+Sp6PeQUN2kb01Mj4n1y1UsqSA
         B6sLr3EZzp99DHI1IgWJRmovjj5vO9sIJ+1CHAJF1rBAWAXC9yxb/p4cbpo6vzomrad8
         GP+qaRcLrQQqs1eDwb9QFYcSMgHovbaUjdLhO8HEQSj5+hwo5sxkEl3SQ04KbHdpWau3
         RwUg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:subject:to:references:from:organization
         :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=MegUZaQqP+Z+mNgI0xq36cxm7P5v8q8y7DI1q/wy1Ck=;
        b=RJIw9QdZbfWrNFeIuMZ+k886ckcUb2UxUFZS44XFcH7YOsLxlM3CnUw/cTzfthrASI
         fkvBlS4e3gFnA6d6Qq6P/nN+6WTf1gUE/CgWj9Mbcs1mY9w06wO8En+f20Q09UjzszXq
         Xw7Sawf9ub7hfyGqyAd5y6vNKd/7lGihPUewyNb8aNsdvU1z9HeVNwIcEZx3+z78Y7XY
         VOy7yGlGtM5eQNgA+H5VTEdb+KYA/ae7EdJ4MnE04ZBGfypz8AHgt8MqapqGpSxAZIX7
         /3nuP+4aEOPhoCIyyDe7YqS4kzQTx2zjB/g5ipqb9tPL/TLQeYp+jn5E+Zh0N8nKe1x6
         LXsw= 
X-Gm-Message-State: AE9vXwNVkDTZ6MD1Mjf0r23/x/oD30y5p9LbLzT87muILPRCMVBq92f9+EyIhh12Up9t7w==
X-Received: by 10.46.69.10 with SMTP id s10mr1732209lja.1.1472402131878;
        Sun, 28 Aug 2016 09:35:31 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.207.197 with SMTP id f188ls591812wmg.5.canary; Sun, 28 Aug
 2016 09:35:30 -0700 (PDT)
X-Received: by 10.194.54.137 with SMTP id j9mr13773086wjp.60.1472402130295;
        Sun, 28 Aug 2016 09:35:30 -0700 (PDT)
Original-Received: from mail-wm0-x233.google.com (mail-wm0-x233.google.com. [2a00:1450:400c:c09::233])
        by mx.google.com with ESMTPS id t73si8336900wme.10.2016.08.28.09.35.30
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Sun, 28 Aug 2016 09:35:30 -0700 (PDT)
Received-SPF: pass (google.com: domain of avi@cloudius-systems.com designates 2a00:1450:400c:c09::233 as permitted sender) client-ip=2a00:1450:400c:c09::233;
Original-Received: by mail-wm0-x233.google.com with SMTP id f65so50320647wmi.0
        for <std-proposals@isocpp.org>; Sun, 28 Aug 2016 09:35:30 -0700 (PDT)
X-Received: by 10.194.88.41 with SMTP id bd9mr9527568wjb.75.1472402129865;
        Sun, 28 Aug 2016 09:35:29 -0700 (PDT)
Original-Received: from avi.cloudius-systems.com ([37.142.229.250])
        by smtp.gmail.com with ESMTPSA id x6sm30113736wjk.26.2016.08.28.09.35.28
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Sun, 28 Aug 2016 09:35:29 -0700 (PDT)
In-Reply-To: <CANh-dXmFXB6WY=uUt6Qh3PzTGek9e0Av-0h4TLnFzXSY6mAR+A@mail.gmail.com>
X-Original-Sender: avi@scylladb.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@scylladb-com.20150623.gappssmtp.com;       spf=pass (google.com:
 domain of avi@cloudius-systems.com designates 2a00:1450:400c:c09::233 as
 permitted sender) smtp.mailfrom=avi@cloudius-systems.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:28008
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/28008>

This is a multi-part message in MIME format.
--------------86C3882CFC8E2D41499A0DDB
Content-Type: text/plain; charset=UTF-8; format=flowed

On 08/25/2016 06:52 PM, 'Jeffrey Yasskin' via ISO C++ Standard - Future 
Proposals wrote:
> On Thu, Aug 25, 2016 at 8:41 AM, Larry Evans 
> <cppljevans@suddenlink.net <mailto:cppljevans@suddenlink.net>> wrote:
>
>     On 08/25/2016 09:12 AM, Ville Voutilainen wrote:
>
>         On 25 August 2016 at 16:47, Dominic Fandrey
>         <kamikaze@bsdforen.de <mailto:kamikaze@bsdforen.de>> wrote:
>
>             I searched this list for similar suggestions, but of
>             course I may
>             have overlooked something.
>
>             I frequently use the following pattern:
>
>                 enum class FooBar { KEKS, DOSE };
>                 char const * const FooBarStr[]{"KEKS", "DOSE"};
>
>             I use the strings to in error messages, verbose output, etc.
>
>                 FooBarStr[static_cast<int>(FooBar::KEKS)] // "KEKS"
>
>             This has some disadvantages, e.g. enums with explicitly
>             stated values
>             may have gaps, negative values and may be defined out of
>             order.
>             Also, changing the enum also means changing the strings
>             manually.
>
>             What I'd like to see:
>
>                 static_cast<char const *>(FooBar::KEKS) // "KEKS"
>
>
>
>         See
>         http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0194r1.html
>         <http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0194r1.html>
>
>     Could you be more specific.  I grepped for enumName, for char const*,
>     and scanned briefly for other strings that might show where
>     conversion from enum to char const* might be, without success :(
>
>
> In that proposal, the operation you want is spelled 
> get_name_v<reflexpr(FooBar::KEKS)>.
>
>

Likely, he wants to index the array with a non-constexpr value.  So he'd 
need code to create a static array (or unordered_map, if the enum is 
sparse) for a dynamic lookup.

Something like

const char* as_string(FooBar v) {
     if constexpr (is_sparse_enum_v<FooBar>) {
         return as_string_sparse(v);
     } else {
         return as_string_dense(b);
     }
}

template <typename Enum>
const char* as_string_dense(Enum v)
     static const char* tab[] = { magic code to populate the table with 
reflexpr(Enum)... };
     return tab[static_cast<std::underlying_type_t<Enum>>(v)];
}

template <typename Enum>
const char* as_string_sparse(Enum v) {
     static std::unordered_map<std::underlying_type_t<Enum>, const 
char*> = { more magic code to populate the table with reflexpr(Enum)... };
     return tab[static_cast<std::underlying_type_t<Enum>>(v)];
}

this is complex enough to merit a library, and common enough to be 
standardized.

-- 
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/1f2b9290-581d-1fec-a98d-5efe4f5f8fc9%40scylladb.com.

--------------86C3882CFC8E2D41499A0DDB
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">
    On 08/25/2016 06:52 PM, 'Jeffrey Yasskin' via ISO C++ Standard -
    Future Proposals wrote:<br>
    <blockquote
cite=3D"mid:CANh-dXmFXB6WY=3DuUt6Qh3PzTGek9e0Av-0h4TLnFzXSY6mAR+A@mail.gmai=
l.com"
      type=3D"cite">
      <div dir=3D"ltr">
        <div class=3D"gmail_extra">
          <div class=3D"gmail_quote">On Thu, Aug 25, 2016 at 8:41 AM,
            Larry Evans <span dir=3D"ltr">&lt;<a moz-do-not-send=3D"true"
                href=3D"mailto:cppljevans@suddenlink.net" target=3D"_blank"
                class=3D"cremed">cppljevans@suddenlink.net</a>&gt;</span>
            wrote:<br>
            <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex"><span
                class=3D"">On 08/25/2016 09:12 AM, Ville Voutilainen
                wrote:<br>
                <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0
                  .8ex;border-left:1px #ccc solid;padding-left:1ex">
                  On 25 August 2016 at 16:47, Dominic Fandrey &lt;<a
                    moz-do-not-send=3D"true"
                    href=3D"mailto:kamikaze@bsdforen.de" target=3D"_blank"
                    class=3D"cremed">kamikaze@bsdforen.de</a>&gt; wrote:<br=
>
                  <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0
                    .8ex;border-left:1px #ccc solid;padding-left:1ex">
                    I searched this list for similar suggestions, but of
                    course I may<br>
                    have overlooked something.<br>
                    <br>
                    I frequently use the following pattern:<br>
                    <br>
                    =C2=A0 =C2=A0 enum class FooBar { KEKS, DOSE };<br>
                    =C2=A0 =C2=A0 char const * const FooBarStr[]{"KEKS", "D=
OSE"};<br>
                    <br>
                    I use the strings to in error messages, verbose
                    output, etc.<br>
                    <br>
                    =C2=A0 =C2=A0 FooBarStr[static_cast&lt;int&gt;(Foo<wbr>=
Bar::KEKS)]
                    // "KEKS"<br>
                    <br>
                    This has some disadvantages, e.g. enums with
                    explicitly stated values<br>
                    may have gaps, negative values and may be defined
                    out of order.<br>
                    Also, changing the enum also means changing the
                    strings manually.<br>
                    <br>
                    What I'd like to see:<br>
                    <br>
                    =C2=A0 =C2=A0 static_cast&lt;char const *&gt;(FooBar::K=
EKS) //
                    "KEKS"<br>
                  </blockquote>
                  <br>
                  <br>
                  See <a moz-do-not-send=3D"true"
                    href=3D"http://open-std.org/JTC1/SC22/WG21/docs/papers/=
2016/p0194r1.html"
                    rel=3D"noreferrer" target=3D"_blank" class=3D"cremed">h=
ttp://open-std.org/JTC1/SC22/<wbr>WG21/docs/papers/2016/p0194r1.<wbr>html</=
a><br>
                  <br>
                </blockquote>
              </span>
              Could you be more specific.=C2=A0 I grepped for enumName, for
              char const*,<br>
              and scanned briefly for other strings that might show
              where<br>
              conversion from enum to char const* might be, without
              success :(</blockquote>
            <div><br>
            </div>
            <div>In that proposal, the operation you want is spelled
              get_name_v&lt;reflexpr(FooBar::KEKS)&gt;.</div>
            <div><br>
            </div>
            <br>
          </div>
        </div>
      </div>
    </blockquote>
    <br>
    Likely, he wants to index the array with a non-constexpr value.=C2=A0 S=
o
    he'd need code to create a static array (or unordered_map, if the
    enum is sparse) for a dynamic lookup.<br>
    <br>
    Something like<br>
    <br>
    const char* as_string(FooBar v) {<br>
    =C2=A0=C2=A0=C2=A0 if constexpr (is_sparse_enum_v&lt;FooBar&gt;) {<br>
    =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return as_string_sparse(v);<=
br>
    =C2=A0=C2=A0=C2=A0 } else {<br>
    =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return as_string_dense(b);<b=
r>
    =C2=A0=C2=A0=C2=A0 }<br>
    }<br>
    <br>
    template &lt;typename Enum&gt;<br>
    const char* as_string_dense(Enum v)<br>
    =C2=A0=C2=A0=C2=A0 static const char* tab[] =3D { magic code to populat=
e the table
    with reflexpr(Enum)... };<br>
    =C2=A0=C2=A0=C2=A0 return
    tab[static_cast&lt;std::underlying_type_t&lt;Enum&gt;&gt;(v)];<br>
    }<br>
    <br>
    template &lt;typename Enum&gt;<br>
    const char* as_string_sparse(Enum v) {<br>
    =C2=A0=C2=A0=C2=A0 static std::unordered_map&lt;std::underlying_type_t&=
lt;Enum&gt;,
    const char*&gt; =3D { more magic code to populate the table with
    reflexpr(Enum)... };<br>
    =C2=A0=C2=A0=C2=A0 return
    tab[static_cast&lt;std::underlying_type_t&lt;Enum&gt;&gt;(v)];<br>
    }<br>
    <br>
    this is complex enough to merit a library, and common enough to be
    standardized.<br>
    <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/1f2b9290-581d-1fec-a98d-5efe4f5f8fc9%=
40scylladb.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.googl=
e.com/a/isocpp.org/d/msgid/std-proposals/1f2b9290-581d-1fec-a98d-5efe4f5f8f=
c9%40scylladb.com</a>.<br />

--------------86C3882CFC8E2D41499A0DDB--

.
