220 26318 <CAJnLdOY4UaYaeVGWVzNa5kwM3P-Gw8P350n7ggVDjyOEHFrC2Q@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: "'Edward Catmur' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: statement folding on variadic templates
 parameters pack
Date: Mon, 13 Jun 2016 15:32:28 +0100
Lines: 173
Approved: news@gmane.org
Message-ID: <CAJnLdOY4UaYaeVGWVzNa5kwM3P-Gw8P350n7ggVDjyOEHFrC2Q@mail.gmail.com>
References: <1e3daa7c-e10a-4589-8118-26f47bc1a0df@isocpp.org>
 <2d8458f8-27b2-464f-a16e-fc866ccfd2cd@isocpp.org> <b448d4b9-c76d-4d27-8b16-0e005478320f@isocpp.org>
 <d90bb3eb-e1d4-4a29-bb7e-63c097f49f96@isocpp.org> <bf02cde4-5da6-4a0b-903f-8b9230d4869c@isocpp.org>
 <CALbYwOymuWcO0WO6aSGCd1iv-dTUxPXP1BMYDdv5Pfcafw_xcQ@mail.gmail.com>
 <CAJnLdOZ5L0P6qdoer6jntOjtYwCTpV_VEGJv6bZUmGk6zmoQ2g@mail.gmail.com>
 <a731ae2e-9519-4d03-81ec-506b7bbda8b5@isocpp.org> <CAFk2RUZBSMsJOkjDaU=9xz4iJ9qNadygbPiB004oCh2p3jfgFg@mail.gmail.com>
 <8dc1e665-42a3-4ef0-a2d2-3773afab170c@isocpp.org> <20160612153937.4898897.23968.12102@gmail.com>
 <57d8c679-f312-4b77-8210-705f5e093b7e@isocpp.org> <CADvuK0+Jon-6hH0JU=xqScearhoMu4eVWieTpvYF-N42ky4Bgg@mail.gmail.com>
 <62812f9c-7c4e-4a97-86a3-aee457c9f4a9@isocpp.org> <CADvuK0LsL5QR9hyC_AGzGEfOuo34AMCt0gFCghtfDJ5+eg6nzw@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a114f3f7c0d9d67053529c215
X-Trace: ger.gmane.org 1465828359 8498 80.91.229.3 (13 Jun 2016 14:32:39 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 13 Jun 2016 14:32:39 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDZLZTXF7UJBB7MH7O5AKGQEPFN7SLA@isocpp.org Mon Jun 13 16:32:34 2016
Return-path: <std-proposals+bncBDZLZTXF7UJBB7MH7O5AKGQEPFN7SLA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ob0-f197.google.com ([209.85.214.197])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDZLZTXF7UJBB7MH7O5AKGQEPFN7SLA@isocpp.org>)
	id 1bCSun-00031J-Sk
	for gclcip-std-proposals@m.gmane.org; Mon, 13 Jun 2016 16:32:34 +0200
Original-Received: by mail-ob0-f197.google.com with SMTP id r6sf401453obx.1
        for <gclcip-std-proposals@m.gmane.org>; Mon, 13 Jun 2016 07:32:33 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=mime-version:in-reply-to:references:from:date:message-id:subject: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=tXcZ/oDJOmExYlezPmYGFYlGFir69X9ZY5nqPva8lb8=;
        b=aSkBNGTYP+ro3LUIWmrqtYnqZYehs54rXqDMSOuUq0KcX3oHhgkzoOsXDLES5eM5S+
         75vPcB5+8k5ZEhuvmxzk4BOxHe11QODzQByCZbUeSfXL6beWa8zkx0YNVXKXu68BfA2z
         zTQQeN8RN5zZEiGnIQo8EQjnB8WV1AbMyJmmdPG3GhAC+Mx6dMOkzbicf+8xcw4Ff8CK
         MfP1mJ3UTB4XI+fWC/Ybl4xAP8Z3UbZwrOpoTsdtcLVDasNEzQnkcFiqv4g0sPmQNkIx
         m2CXM/GVbb0AOyFX0GCiNZ7GrdjqoiP2Zbhv1gKlM8zs91YIAWzRQrVJYLiSmykRZ242
         hemA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:from:date
         :message-id:subject: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=tXcZ/oDJOmExYlezPmYGFYlGFir69X9ZY5nqPva8lb8=;
        b=lP8eT8Mjkn0/K48b+Sx0CuBjfjqUDHRlqsASHPz0s1xHvHtZ3sqHV/nQVeru8b98MG
         hcFr5S7HK3OzlXYT6MfHK+LaDgJSANg2Up4+kHmtibWlq/QJbLtx3B8n+3EHginAOqXf
         JvojuzEINKpT18isqxKy1++KY3KBrEo/bKTwKPfdppZKNKVjN5vwctP0I5qBDYPTdAr8
         RwANoCDjsy/n+VP54xGRLWQAXMTWRIWbDvYGwnCSA8DaklNh28jCYeoKGSz8+r0rQvYz
         6GrOgnnX6wUHAYaAJts57lLdb15jKIBJ8/PLCBcAOpJBX4TYZ1fhOm9c9oEdr67QQVrH
         cA+A==
X-Gm-Message-State: ALyK8tLnphnAuYud7oNeG0hj4m3N5BMWBPgXYbC5QJjBnZS99abhuYQXoPMSrXHYDo8H5Q==
X-Received: by 10.157.3.76 with SMTP id 70mr6343381otv.41.1465828352955;
        Mon, 13 Jun 2016 07:32:32 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.18.103 with SMTP id 94ls3332531qge.60.gmail; Mon, 13 Jun
 2016 07:32:29 -0700 (PDT)
X-Received: by 10.140.153.135 with SMTP id 129mr14907864qhz.71.1465828349353;
        Mon, 13 Jun 2016 07:32:29 -0700 (PDT)
Original-Received: from mail-qk0-x22f.google.com (mail-qk0-x22f.google.com. [2607:f8b0:400d:c09::22f])
        by mx.google.com with ESMTPS id o202si15346004qke.46.2016.06.13.07.32.29
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Mon, 13 Jun 2016 07:32:29 -0700 (PDT)
Received-SPF: pass (google.com: domain of ecatmur@googlemail.com designates 2607:f8b0:400d:c09::22f as permitted sender) client-ip=2607:f8b0:400d:c09::22f;
Original-Received: by mail-qk0-x22f.google.com with SMTP id a186so19889735qkf.0
        for <std-proposals@isocpp.org>; Mon, 13 Jun 2016 07:32:29 -0700 (PDT)
X-Received: by 10.233.237.129 with SMTP id c123mr14679472qkg.136.1465828349155;
 Mon, 13 Jun 2016 07:32:29 -0700 (PDT)
Original-Received: by 10.237.55.198 with HTTP; Mon, 13 Jun 2016 07:32:28 -0700 (PDT)
In-Reply-To: <CADvuK0LsL5QR9hyC_AGzGEfOuo34AMCt0gFCghtfDJ5+eg6nzw@mail.gmail.com>
X-Original-Sender: ecatmur@googlemail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@googlemail.com;       spf=pass (google.com: domain of
 ecatmur@googlemail.com designates 2607:f8b0:400d:c09::22f as permitted
 sender) smtp.mailfrom=ecatmur@googlemail.com;       dmarc=pass (p=QUARANTINE
 dis=NONE) header.from=googlemail.com
X-Original-From: Edward Catmur <ecatmur@googlemail.com>
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: 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:26318
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/26318>

--001a114f3f7c0d9d67053529c215
Content-Type: text/plain; charset=UTF-8

On Mon, Jun 13, 2016 at 4:11 AM, Arthur O'Dwyer <arthur.j.odwyer@gmail.com>
wrote:

> On Sun, Jun 12, 2016 at 6:17 PM, Edward Catmur <ed@catmur.co.uk> wrote:
>
>> On Sunday, 12 June 2016 20:32:22 UTC+1, Arthur O'Dwyer wrote:
>>>
>>> I found the docs for Boost.Switch
>>> <http://dancinghacker.com/switch/boost_switch/switch_.html>. If I get
>>> the time, I'll try to write up a modern library implementation of the above
>>> make_switch and post it here. I'm sure it's been done before, of
>>> course, so if someone beats me to it that's great.
>>>
>>
>> Too many times to count, I'm sure! I decided to take another go at it,
>> trying to conform to the Boost.Switch interface but using modern
>> techniques, using only the standard library and Boost.Preprocessor for
>> iteration. My implementation is a little under 60 lines:
>> https://gist.github.com/ecatmur/7f5f8b44e70f414742e4eae1efdd0ca7
>>
>> Interesting!  My implementation ended up at twice the length of yours,
> but IMHO has a nicer user interface than Boost.Switch.
>
> https://gist.github.com/Quuxplusone/3f7100f72551f6a7028844b298c43508
>
> The "apply" example in my case looks like this: extra boilerplate to deal
> with making index sequences, but the tradeoff is that my interface allows
> you to switch on things that aren't consecutive integers.
>

Actually, mine can switch on any integer sequence; it uses the preprocessor
iteration over N to index into the integer sequence for the case label
constant (using std::tuple_element, since that's the only available
facility in the standard library for variadic indexing).

The other tradeoff is that my Clang isn't smart enough to turn this into a
> switch. It will go as far as inlining everything into an if-else chain (no
> function calls), but it won't make a jump table. Yes, this kind of defeats
> the purpose; but I'm holding out hope that it's a Quality of Implementation
> issue rather than a fundamental flaw.
>

Yes, it's a bit disappointing that the free compilers are unable to turn a
recursive if chain into a jump table. Using a syntactic switch has other
benefits, though; it allows the compiler to automatically detect repeated
cases, and also to detect omitted cases where the controlling type is an
enum.


> The "enum to integer" example in your gist doesn't compile on my Clang
> because it is trying to instantiate std::integer_sequence<E, ...> where E (being
> an enum) is not an integral type; I think you have to add some boilerplate
> involving std::conditional_t<std::is_enum_v<E>,
> std::underlying_type_t<E>, E> and some explicit casts, at which point it
> doesn't even seem worth it anymore.
>

Hm. Clang works fine for me in versions 3.5 upward in -std=c++14 mode.


> Having to create an entity of the form foo<..., E::A, E::B, E::C> should
> have been a red flag anyway, IMHO.
>

That sequence would usually be provided by an introspection library
(BetterEnums, or whatever SG7 are up to these days), or by a code
generator.

-- 
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/CAJnLdOY4UaYaeVGWVzNa5kwM3P-Gw8P350n7ggVDjyOEHFrC2Q%40mail.gmail.com.

--001a114f3f7c0d9d67053529c215
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On M=
on, Jun 13, 2016 at 4:11 AM, Arthur O&#39;Dwyer <span dir=3D"ltr">&lt;<a hr=
ef=3D"mailto:arthur.j.odwyer@gmail.com" target=3D"_blank">arthur.j.odwyer@g=
mail.com</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"><div dir=
=3D"ltr"><span class=3D""><div>On Sun, Jun 12, 2016 at 6:17 PM, Edward Catm=
ur <span dir=3D"ltr">&lt;<a href=3D"mailto:ed@catmur.co.uk" target=3D"_blan=
k">ed@catmur.co.uk</a>&gt;</span> wrote:<br></div></span><div class=3D"gmai=
l_extra"><div class=3D"gmail_quote"><span class=3D""><blockquote class=3D"g=
mail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-=
left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div =
dir=3D"ltr"><span>On Sunday, 12 June 2016 20:32:22 UTC+1, Arthur O&#39;Dwye=
r  wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8e=
x;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-styl=
e:solid;padding-left:1ex"><div dir=3D"ltr"><div><div class=3D"gmail_quote">=
<div>I found <a href=3D"http://dancinghacker.com/switch/boost_switch/switch=
_.html" rel=3D"nofollow" target=3D"_blank">the docs for Boost.Switch</a>. I=
f I get the time, I&#39;ll try to write up a modern library implementation =
of the above <font face=3D"monospace, monospace">make_switch</font>=C2=A0an=
d post it here. I&#39;m sure it&#39;s been done before, of course, so if so=
meone beats me to it that&#39;s great.</div></div></div></div></blockquote>=
<div><br></div></span><div>Too many times to count, I&#39;m sure! I decided=
 to take another go at it, trying to conform to the Boost.Switch interface =
but using modern techniques, using only the standard library and Boost.Prep=
rocessor for iteration. My implementation is a little under 60 lines:=C2=A0=
<a href=3D"https://gist.github.com/ecatmur/7f5f8b44e70f414742e4eae1efdd0ca7=
" target=3D"_blank">https://gist.github.com/ecatmur/7f5f8b44e70f414742e4eae=
1efdd0ca7</a>=C2=A0</div><div><br></div></div></blockquote></span>Interesti=
ng!=C2=A0 My implementation ended up at twice the length of yours, but IMHO=
 has a nicer user interface than Boost.Switch.<div><br></div><div><a href=
=3D"https://gist.github.com/Quuxplusone/3f7100f72551f6a7028844b298c43508" t=
arget=3D"_blank">https://gist.github.com/Quuxplusone/3f7100f72551f6a7028844=
b298c43508<br></a></div><div><br></div><div>The &quot;apply&quot; example i=
n my case looks like this: extra boilerplate to deal with making index sequ=
ences, but the tradeoff is that my interface allows you to switch on things=
 that aren&#39;t consecutive integers.</div></div></div></div></blockquote>=
<div><br></div><div>Actually, mine can switch on any integer sequence; it u=
ses the preprocessor iteration over N to index into the integer sequence fo=
r the case label constant (using std::tuple_element, since that&#39;s the o=
nly available facility in the standard library for variadic indexing).</div=
><div><br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8e=
x;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div class=
=3D"gmail_extra"><div class=3D"gmail_quote"><div>The other tradeoff is that=
 my Clang isn&#39;t smart enough to turn this into a switch. It will go as =
far as inlining everything into an if-else chain (no function calls), but i=
t won&#39;t make a jump table. Yes, this kind of defeats the purpose; but I=
&#39;m holding out hope that it&#39;s a Quality of Implementation issue rat=
her than a fundamental flaw.</div></div></div></div></blockquote><div><br><=
/div><div>Yes, it&#39;s a bit disappointing that the free compilers are una=
ble to turn a recursive if chain into a jump table. Using a syntactic switc=
h has other benefits, though; it allows the compiler to automatically detec=
t repeated cases, and also to detect omitted cases where the controlling ty=
pe is an enum.</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" styl=
e=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div di=
r=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote"><div></div>=
<div>The &quot;enum to integer&quot; example in your gist doesn&#39;t compi=
le on my Clang because it is trying to instantiate=C2=A0<font face=3D"monos=
pace, monospace">std::integer_sequence&lt;E, ...&gt;</font>=C2=A0where=C2=
=A0<font face=3D"monospace, monospace">E</font>=C2=A0(being an enum) is not=
 an integral type; I think you have to add some boilerplate involving=C2=A0=
<font face=3D"monospace, monospace">std::conditional_t&lt;std::is_enum_v&lt=
;E&gt;, std::underlying_type_t&lt;E&gt;, E&gt;</font>=C2=A0and some explici=
t casts, at which point it doesn&#39;t even seem worth it anymore. </div></=
div></div></div></blockquote><div><br></div><div>Hm. Clang works fine for m=
e in versions 3.5 upward in -std=3Dc++14 mode.</div><div>=C2=A0</div><block=
quote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc=
 solid;padding-left:1ex"><div dir=3D"ltr"><div class=3D"gmail_extra"><div c=
lass=3D"gmail_quote"><div>Having to create an entity of the form=C2=A0<font=
 face=3D"monospace, monospace">foo&lt;..., E::A, E::B, E::C&gt;</font>=C2=
=A0should have been a red flag anyway, IMHO.</div></div></div></div></block=
quote><div><br></div><div>That sequence would usually be provided by an int=
rospection library (BetterEnums, or whatever SG7 are up to these days), or =
by a code generator.=C2=A0</div></div></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/CAJnLdOY4UaYaeVGWVzNa5kwM3P-Gw8P350n7=
ggVDjyOEHFrC2Q%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAJnLdOY4UaYaeVGW=
VzNa5kwM3P-Gw8P350n7ggVDjyOEHFrC2Q%40mail.gmail.com</a>.<br />

--001a114f3f7c0d9d67053529c215--

.
