public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Евгений Никишин via libstdc++" <libstdc++@gcc.gnu.org>
To: libstdc++@gcc.gnu.org
Subject: Throwing bad_exception when calling current_exception()
Date: Fri, 15 Nov 2019 17:02:00 -0000	[thread overview]
Message-ID: <1573837346.91422813@f701.i.mail.ru> (raw)


Link  https://en.cppreference.com/w/cpp/error/current_exception  provides the following description of  current_exception() :
>If called during exception handling (typically, in a catch clause), captures the current exception object and creates an std::exception_ptr that holds either a copy or a reference to that exception object (depending on the implementation).
>...
>If the implementation of this function requires copying the captured exception object and its copy constructor throws an exception, the returned pointer will hold a reference to the exception thrown. If the copy constructor of the thrown exception object also throws, the returned pointer may hold a reference to an instance of std::bad_exception to break the endless loop.
I am trying to find out if the implementation of  current_exception()  in GCC7 copies captured exception object, or just returns the reference to an already existing object. So far, I think that GCC implements the second case. I've tried to check it by executing the following code:
class my_copy_exception :public exception
{public:
    my_copy_exception (): exception (){}
    my_copy_exception (const my_copy_exception& other): 
      exception(other){throw my_copy_exception();}constchar* what ()constthrow(){return"my_copy_exception";}};int main(){try{throw my_copy_exception();}catch(const exception& e){
    cout << e.what()<< endl;
    exception_ptr eptr = current_exception();try{
      rethrow_exception(eptr);}catch(const std::exception& en){
      cout << en.what()<< endl;
      exception_ptr eptrn = current_exception();

      cout <<(eptr == eptrn)<< endl;}}}
It produces the following output:
my_copy_exception
my_copy_exception
1
Whether it is possible to claim that there is no copying of the exception object? If not, how to make  current_exception()  throw  bad_exception ?

             reply	other threads:[~2019-11-15 17:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-15 17:02 Евгений Никишин via libstdc++ [this message]
2019-12-02 14:39 ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1573837346.91422813@f701.i.mail.ru \
    --to=libstdc++@gcc.gnu.org \
    --cc=kisha-nik@mail.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).