Document number: N2179=07-0039
Programming Language C++, Evolution Subgroup
 
Peter Dimov, <pdimov@pdimov.com>
 
2007-03-03

Language Support for Transporting Exceptions between Threads

I. Overview

This document proposes a minimal and complete addition to the C++ standard that enables a library to move an exception from one thread to another. The interface is essentially a slight reformulation of Option 2 suggested in N2107, Exception Propagation across Threads, by Jens Maurer and Alisdair Meredith.

II. Proposed Text

Add to the synopsis of <exception> the following:

namespace std {

    typedef unspecified exception_ptr;

    exception_ptr current_exception();
    void rethrow_exception( exception_ptr p );

    template< class E > exception_ptr copy_exception( E e );

}

Add another section, Exception Propagation [propagation], after [uncaught] with the following contents:

typedef unspecified exception_ptr;

The type exception_ptr can be used to refer to an exception.

exception_ptr shall be DefaultConstructible, CopyConstructible, Assignable and EqualityComparable. exception_ptr's operations do not throw.

Two instances of exception_ptr are equivalent and compare equal if and only if they refer to the same exception.

The default constructor of exception_ptr produces the null value of the type. The null value is equivalent only to itself.

exception_ptr can be compared for equality with a null pointer constant or assigned a null pointer constant. The effect shall be as if exception_ptr() was used in place of the null pointer constant.

[Note: An implementation might use a reference-counted smart pointer as exception_ptr.]

exception_ptr current_exception();

Returns: An exception_ptr that refers to the currently handled exception or a copy of the currently handled exception, or a null exception_ptr if no exception is being handled. If the function needs to allocate memory and the attempt fails, it returns an exception_ptr that refers to an instance of bad_alloc. It is unspecified whether the return values of two successive calls to current_exception refer to the same exception.

[Note: that is, it is unspecified whether current_exception creates a new copy each time it is called.]

Throws: nothing.

void rethrow_exception( exception_ptr p );

Requires: p shall not be null.

Throws: the exception to which p refers.

template< class E > exception_ptr copy_exception( E e );

Effects: as if try { throw e; } catch( ... ) { return current_exeption(); }.

[Note: This function is provided for convenience and efficiency reasons.]

--end