220 33546 <f85a2e67-4d76-4dac-9e45-a560e3e8872f@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Edward Catmur <ed@catmur.co.uk>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: Catch block for multiple exceptions
Date: Wed, 2 Aug 2017 13:05:33 -0700 (PDT)
Lines: 202
Approved: news@gmane.org
Message-ID: <f85a2e67-4d76-4dac-9e45-a560e3e8872f@isocpp.org>
References: <effe744d-f832-42c7-9b2a-ef31551067d0@isocpp.org>
 <deaedeb0-a699-49e9-826a-c41e674902c0@isocpp.org> <CAEfefmzCWDwP2ha7UfvvPrW_56TT4DEgvwRyxWm=6gS_GZc7pA@mail.gmail.com>
 <4126837.jZlGhgVnua@tjmaciei-mobl1> <d14636de-fb57-47b3-8666-7976ccdb6fe8@isocpp.org>
 <d58143f8-c128-3745-b835-1e04a5760d0d@gmail.com> <CA+fGSbMB3=H4hetWu3GFPzUskLP_sh0Q=Q4oxsUsZ6ci-jrpxg@mail.gmail.com>
 <CAFk2RUbdEQMQB4qLCDeaHeF0Q4ntbZG7-Cuf0X2G8d5+Dpf1Pw@mail.gmail.com> <aec5f733-d43e-4d75-b001-0a0e9d49217f@isocpp.org>
 <CAFk2RUbmCYrr8eSKDHq6GDV-VhwPZoPM07Je6r5VPugEhrfpig@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_6499_559883321.1501704333569"
X-Trace: blaine.gmane.org 1501704336 5906 195.159.176.226 (2 Aug 2017 20:05:36 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Wed, 2 Aug 2017 20:05:36 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDZLZTXF7UJBBDXBRDGAKGQEY7GGB2A@isocpp.org Wed Aug 02 22:05:31 2017
Return-path: <std-proposals+bncBDZLZTXF7UJBBDXBRDGAKGQEY7GGB2A@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-qt0-f199.google.com ([209.85.216.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDZLZTXF7UJBBDXBRDGAKGQEY7GGB2A@isocpp.org>)
	id 1dczta-0001DL-8k
	for gclcip-std-proposals@m.gmane.org; Wed, 02 Aug 2017 22:05:30 +0200
Original-Received: by mail-qt0-f199.google.com with SMTP id u19sf25689548qtc.14
        for <gclcip-std-proposals@m.gmane.org>; Wed, 02 Aug 2017 13:05:36 -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=p2RytbtSlnY2AEx8365QQUg2pyG8SoyUT+iK1R/kzX8=;
        b=m1+KIsEe804GDVGNh9CERvg1gRnWB3WGmGhoXs0DBG+a4w97+mFVLxUp6+guNYAJBb
         1DpbGVP1/OhqkSL4fdNui9DyWHmR24rhtF70XVp4TAjrVQsScmVip/W4fCVWSexLBMHe
         diPdY1515uJQ4bhGIC6jlPNqfr/H0F4747PTKAXrb15QXKaG+ykikYrdVWpqnurb8zIi
         RN9mJ6mdY84ekRQGKetbFXfukdaBzKZSKXZNo+id8sXWBdWTwaKYNYEx7T6hW/IDMiXx
         E0k7P15Ul/Z64+/nHi55UTklKu1Qt7tE3QoH8SPly4+NSHuhwBRDfevlqLgwTGFQOoem
         FJxg==
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=p2RytbtSlnY2AEx8365QQUg2pyG8SoyUT+iK1R/kzX8=;
        b=SCtSX3s6Y80GkeUZuCvjgzd2zrGWM+9GfP+hbzB7oKMkkAs+thqN2nOnuqZaqieQz5
         IfP/ah+UjN/UvTmPwyvngUmoSsQSRt0v8SqmXaxu/nNNnKivoYyyy3DdLECQDbLYpVuG
         7woKFCEVZKebepxML3zyJovH7Q8JNixARWbaMUGfzafPszRE7IlN/qSw9QNidsPUNe2c
         5hyEjDP6k+dxXDCXui4KBusjebCJZBEFr/HhEE9HiE16RNNc+e3NkwKkzjMh7P+XKInw
         QhH98TmyyIFEaJ8v4Cf8RWFC/EILpjekAL04Udmh9iM8QQhYf38GRleMT6pYqGaC2WrJ
         tmkA==
X-Gm-Message-State: AIVw111WGdr//gb5Hari3Ah9XRYzYDKSMjFrFmsMUXN0aZ0U+fIucgBg
	g9KHGgnIfbluQwTm
X-Received: by 10.237.62.245 with SMTP id o50mr16966649qtf.35.1501704335971;
        Wed, 02 Aug 2017 13:05:35 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.36.111.9 with SMTP id x9ls131660itb.16.gmail; Wed, 02 Aug 2017
 13:05:34 -0700 (PDT)
X-Received: by 10.31.151.77 with SMTP id z74mr101408vkd.3.1501704334183;
        Wed, 02 Aug 2017 13:05:34 -0700 (PDT)
In-Reply-To: <CAFk2RUbmCYrr8eSKDHq6GDV-VhwPZoPM07Je6r5VPugEhrfpig@mail.gmail.com>
X-Original-Sender: ed@catmur.co.uk
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:33546
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/33546>

------=_Part_6499_559883321.1501704333569
Content-Type: multipart/alternative; 
	boundary="----=_Part_6500_271610059.1501704333570"

------=_Part_6500_271610059.1501704333570
Content-Type: text/plain; charset="UTF-8"

On Wednesday, 2 August 2017 20:07:03 UTC+1, Ville Voutilainen wrote:
>
> On 2 August 2017 at 21:47, Nicol Bolas <jmck...@gmail.com <javascript:>> 
> wrote: 
> > However, I think a small library feature could help make it less 
> painful. 
> > Right now, we have `current_exception`, which gives us an 
> `exception_ptr`, 
> > which is completely type-erased. We could give `exception_ptr` a way to 
> > un-erase the type. After all, the implementation has to have that type 
> > somewhere, since we can throw and re-catch it. 
> > 
> > So perhaps an `any_cast`-like mechanism, that returns either `nullptr` 
> or a 
> > pointer to the exception object. Only it should work as if it were a 
> > `dynamic_cast`. Then, we could implement an `exception_visitor` that 
> takes a 
> > sequence of types and tries them, in order. 
>
> The ability to do an any_cast on an exception_ptr would indeed be 
> nice. I'll see what I can 
> do. There's a chance that adding such a beast isn't particularly hard 
> implementation-wise. 
>

For libstdc++ it's trivial:

template<class target>
target_type* exception_cast(exception_ptr& ep, bool exact = false) {
    void* p = ep._M_get();
    std::typeinfo target_type = typeid(target), exc_type = ep.
__cxa_exception_type();
    return (exact ? target_type == exc_type : target_type.__do_catch(
exc_type, &p, 1)) ?
        static_cast<target*>(p) :
        nullptr;
}

I suppose MSVC might require a bit more work though. I remember when I last 
looked at Windows exception handling (trying to peek at the typeid of an 
unknown exception, no __cxa_exception_type there) it got hairy pretty fast.

-- 
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/f85a2e67-4d76-4dac-9e45-a560e3e8872f%40isocpp.org.

------=_Part_6500_271610059.1501704333570
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Wednesday, 2 August 2017 20:07:03 UTC+1, Ville Voutilai=
nen  wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left=
: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On 2 August 2017 at=
 21:47, Nicol Bolas &lt;<a href=3D"javascript:" target=3D"_blank" gdf-obfus=
cated-mailto=3D"fEsDtXjaBQAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D&=
#39;javascript:&#39;;return true;" onclick=3D"this.href=3D&#39;javascript:&=
#39;;return true;">jmck...@gmail.com</a>&gt; wrote:
<br>&gt; However, I think a small library feature could help make it less p=
ainful.
<br>&gt; Right now, we have `current_exception`, which gives us an `excepti=
on_ptr`,
<br>&gt; which is completely type-erased. We could give `exception_ptr` a w=
ay to
<br>&gt; un-erase the type. After all, the implementation has to have that =
type
<br>&gt; somewhere, since we can throw and re-catch it.
<br>&gt;
<br>&gt; So perhaps an `any_cast`-like mechanism, that returns either `null=
ptr` or a
<br>&gt; pointer to the exception object. Only it should work as if it were=
 a
<br>&gt; `dynamic_cast`. Then, we could implement an `exception_visitor` th=
at takes a
<br>&gt; sequence of types and tries them, in order.
<br>
<br>The ability to do an any_cast on an exception_ptr would indeed be
<br>nice. I&#39;ll see what I can
<br>do. There&#39;s a chance that adding such a beast isn&#39;t particularl=
y hard
<br>implementation-wise.
<br></blockquote><div><br></div><div>For libstdc++ it&#39;s trivial:</div><=
div><br></div><div class=3D"prettyprint" style=3D"background-color: rgb(250=
, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-=
width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=
=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-colo=
r: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wrap: b=
reak-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">template</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">class</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> target</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">&gt;</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br>target_type</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">*</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> exception_cast</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">exception_ptr</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&amp;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> ep</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">b=
ool</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> exact =
</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 sty=
le=3D"color: #008;" class=3D"styled-by-prettify">false</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;" cla=
ss=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">void</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">*</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> p </span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> ep<=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">_M_get</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">();</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 std</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">typeinfo target_type </span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">typeid</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify">target</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">),</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> exc_type </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> ep</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">__cxa_=
exception_type</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">();</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify">exact </span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">?</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> target_type </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">=3D=3D</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> exc_type </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> target_type</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify">__do_catch</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">exc_type</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">&amp;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>p</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span sty=
le=3D"color: #066;" class=3D"styled-by-prettify">1</span><span style=3D"col=
or: #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">?</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color:=
 #008;" class=3D"styled-by-prettify">static_cast</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">target</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">*&gt;(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">p</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </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=A0 =C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">nullptr</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><=
br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span=
></div></code></div></code></div><div><br></div><div>I suppose MSVC might r=
equire a bit more work though. I remember when I last looked at Windows exc=
eption handling (trying to peek at the typeid of an unknown exception, no _=
_cxa_exception_type there) it got hairy pretty fast.</div><div><br></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/f85a2e67-4d76-4dac-9e45-a560e3e8872f%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/f85a2e67-4d76-4dac-9e45-a560e3e8872f=
%40isocpp.org</a>.<br />

------=_Part_6500_271610059.1501704333570--

------=_Part_6499_559883321.1501704333569--

.
