Topic: [exception_ptr] exception_ptr_cast: cast to the


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Sat, 04 Jan 2014 12:45:48 +0100
Raw View
This is a multi-part message in MIME format.
--------------090606000102070200000607
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

I'm working on a proposal of an expected<T> class based on the
Expected<T> from Alexandrescu.
Expected<T> provides a function hasException defined as follows:

template <class E>
bool hasException() const {
   try {
      if (!gotHam) std::rethrow_exception(spam);
   } catch (const E& object) {
      return true;
   } catch (...) {
   }
   return false;
}

But this function doesn't allow to get the stored exception so that we
cannot access specifics members in exception E.

if (y.hasException<NotDivisible>())
{
.... // no way to get the values that are not dividible
}

Of course, the user could always do

   try {
      if (! y.valid()) std::rethrow_exception(y.exception_ptr());
   } catch (const NotDivisible& ex) {
std::cout <<ex.num <<"," <<ex.den<<std::endl;
   } catch (...) {
   }

But, this is not optimal, as we need to use a try-catch block and
rethrow the exception.

I would like to define a dynamic cast that returns a pointer to the
stored exception it the type match

template <class E, class T>
const E* expected_cast(const expected<T>*);

if (auto ex = expected_cast<NotDivisible*>(&y))
{
std::cout <<ex->num <<"," <<ex->den<<std::endl;
}

But I don't see how to implement it without having the possibility to
cast the exception_ptr instance to the stored exception. Do you know how
this can be implemented with the current exception_ptr interface?

If instead of storing the exception on a exception_ptr, we store it on
the std::experimental::any we can use std::experimental::any_cast to get
the stored exception. However, while std::experimental::any could be
used to store any explicit exception, it cannot be used to store the
current exception.

Is there a way to define a exception_ptr_cast (similar to the one of
std::experimental::any_cast) for std::exception_ptr?
Are there some restrictions to the stored exceptions?
It is worth writing a standard proposal?

Best,
Vicente


--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

--------------090606000102070200000607
Content-Type: text/html; charset=ISO-8859-1

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hi,<br>
    <br>
    I'm working on a proposal of an expected&lt;T&gt; class based on the
    Expected&lt;T&gt; from Alexandrescu.<br>
    Expected&lt;T&gt; provides a function hasException defined as
    follows:<br>
    <br>
    <meta http-equiv="Content-Type" content="text/html;
      charset=ISO-8859-1">
    <title></title>
    template &lt;class E&gt;<br>
    bool hasException() const {<br>
    &nbsp; try {<br>
    &nbsp; &nbsp;&nbsp; if (!gotHam) std::rethrow_exception(spam);<br>
    &nbsp; } catch (const E&amp; object) {<br>
    &nbsp; &nbsp;&nbsp; return true;<br>
    &nbsp; } catch (...) {<br>
    &nbsp; }<br>
    &nbsp; return false;<br>
    }<br>
    <br>
    But this function doesn't allow to get the stored exception so that
    we cannot access specifics members in exception E.<br>
    <br>
    if (y.hasException&lt;NotDivisible&gt;()) <br>
    {<br>
    ... // no way to get the values that are not dividible<br>
    }<br>
    <br>
    Of course, the user could always do<br>
    <br>
    &nbsp; try {<br>
    &nbsp; &nbsp;&nbsp; if (! y.valid()) std::rethrow_exception(y.exception_ptr());<br>
    &nbsp; } catch (const NotDivisible&amp; ex) {<br>
    &nbsp;&nbsp;&nbsp;&nbsp;
    std::cout &lt;&lt;ex.num &lt;&lt;","
    &lt;&lt;ex.den&lt;&lt;std::endl;<br>
    &nbsp; } catch (...) {<br>
    &nbsp; }<br>
    <br>
    But, this is not optimal, as we need to use a try-catch block and
    rethrow the exception.<br>
    <br>
    I would like to define a dynamic cast that returns a pointer to the
    stored exception it the type match<br>
    <br>
    template &lt;class E, class T&gt;<br>
    const E* expected_cast(const expected&lt;T&gt;*);<br>
    <br>
    if (auto ex = expected_cast&lt;NotDivisible*&gt;(&amp;y)) <br>
    {<br>
    &nbsp;&nbsp;
    std::cout &lt;&lt;ex-&gt;num &lt;&lt;"," &lt;&lt;ex-&gt;den&lt;&lt;std::endl;<br>
    }<br>
    <br>
    But I don't see how to implement it without having the possibility
    to cast the exception_ptr instance to the stored exception. Do you
    know how this can be implemented with the current exception_ptr
    interface?<br>
    <br>
    If instead of storing the exception on a exception_ptr, we store it
    on the std::experimental::any we can use std::experimental::any_cast
    to get the stored exception. However, while std::experimental::any
    could be used to store any explicit exception, it cannot be used to
    store the current exception.<br>
    <br>
    Is there a way to define a exception_ptr_cast (similar to the one of
    std::experimental::any_cast) for std::exception_ptr?<br>
    Are there some restrictions to the stored exceptions?<br>
    It is worth writing a standard proposal?<br>
    <br>
    Best,<br>
    Vicente<br>
    <br>
    <br>
  </body>
</html>

<p></p>

-- <br />
&nbsp;<br />
--- <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 email to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />

--------------090606000102070200000607--

.


Author: Cassio Neri <cassio.neri@gmail.com>
Date: Mon, 6 Jan 2014 07:33:23 -0800 (PST)
Raw View
------=_Part_97_26744951.1389022403263
Content-Type: text/plain; charset=ISO-8859-1



On Saturday, January 4, 2014 11:45:48 AM UTC, Vicente J. Botet Escriba
wrote:
>
>
> I would like to define a dynamic cast that returns a pointer to the stored
> exception it the type match
>
> template <class E, class T>
> const E* expected_cast(const expected<T>*);
>
> if (auto ex = expected_cast<NotDivisible*>(&y))
> {
>    std::cout <<ex->num <<"," <<ex->den<<std::endl;
> }
>
>
I'm not sure but what about something along these lines?

const E* expected_cast(const expected<T>* y) {
  try {
    if (!y->valid())
        std::rethrow_exception(y->exception_ptr());
  } catch (const E& object) {
     return &obj;
  } catch (...) {
  }
  return nullptr;
}

Is there any lifetime issue here? What if a similar function was part of
the exceptr_ptr interface?

HTH
Cassio.

--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_97_26744951.1389022403263
Content-Type: text/html; charset=ISO-8859-1

<div dir="ltr"><br><br>On Saturday, January 4, 2014 11:45:48 AM UTC, Vicente J. Botet Escriba wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">




  <div bgcolor="#FFFFFF" text="#000000">
    <br>
    I would like to define a dynamic cast that returns a pointer to the
    stored exception it the type match<br>
    <br>
    template &lt;class E, class T&gt;<br>
    const E* expected_cast(const expected&lt;T&gt;*);<br>
    <br>
    if (auto ex = expected_cast&lt;NotDivisible*&gt;(&amp;<wbr>y)) <br>
    {<br>
    &nbsp;&nbsp;
    std::cout &lt;&lt;ex-&gt;num &lt;&lt;"," &lt;&lt;ex-&gt;den&lt;&lt;std::endl;<br>
    }<br>
    <br></div></blockquote><div><br>I'm not sure but what about something along these lines?<br><br>const E* expected_cast(const expected&lt;T&gt;* y) {<br>&nbsp; try {<br>&nbsp;&nbsp;&nbsp; if (!y-&gt;valid())<br>
    &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::rethrow_exception(y-&gt;exception_ptr());<br>
    &nbsp; } catch (const E&amp; object) {<br>
    &nbsp; &nbsp;&nbsp; return &amp;obj;<br>
    &nbsp; } catch (...) {<br>
    &nbsp; }<br>
    &nbsp; return nullptr;<br>
    }<br><br>Is there any lifetime issue here? What if a similar function was part of the exceptr_ptr interface?<br><br>HTH<br>Cassio.<br></div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <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 email to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />

------=_Part_97_26744951.1389022403263--

.