public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* Throwing bad_exception when calling current_exception()
@ 2019-11-15 17:02 Евгений Никишин via libstdc++
  2019-12-02 14:39 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Евгений Никишин via libstdc++ @ 2019-11-15 17:02 UTC (permalink / raw)
  To: libstdc++


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 ?

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Throwing bad_exception when calling current_exception()
  2019-11-15 17:02 Throwing bad_exception when calling current_exception() Евгений Никишин via libstdc++
@ 2019-12-02 14:39 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2019-12-02 14:39 UTC (permalink / raw)
  To: Евгений
	Никишин
  Cc: libstdc++

On 15/11/19 20:02 +0300, Евгений Никишин via libstdc++ wrote:
>
>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 ?

Sorry for the delay, I didn't receive this email due to some problems
with our email systems.

The libstdc++ implementation never copies the exception. A reference
to the existing one is returned (and the reference count increased).


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-12-02 14:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-15 17:02 Throwing bad_exception when calling current_exception() Евгений Никишин via libstdc++
2019-12-02 14:39 ` Jonathan Wakely

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).