Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 21994

Throwing bad_exception when calling current_exception()

$
0
0

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();
      }

    const char* what () const throw() {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?


Viewing all articles
Browse latest Browse all 21994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>