Title
Move construction of thrown object
Status
cd2
Section
11.4.5.3 [class.copy.ctor]
Submitter
Steve Adamczyk

Created on 2009-05-06.00:00:00 last changed 171 months ago

Messages

Date: 2010-03-15.00:00:00

[Voted into WP at March, 2010 meeting.]

Date: 2009-10-15.00:00:00

Proposed resolution (October, 2009):

Change 11.4.5.3 [class.copy.ctor] paragraph 17 second bullet as follows:

  • in a throw-expression, when the operand is the name of a non-volatile automatic object whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy operation from the operand to the exception object (14.2 [except.throw]) can be omitted by constructing the automatic object directly into the exception object
Date: 2009-07-15.00:00:00

Notes from the July, 2009 meeting:

It is not clear how important this optimization is in the context of throw: how often is a large object with substantial copying overhead thrown? Also, throwing an exception is already a heavyweight operation, so presumably moving instead of copying an object would not make much difference.

Date: 2022-02-18.07:47:23

11.4.5.3 [class.copy.ctor] paragraph 16 details the conditions under which a thrown object can be moved instead of copied. However, the optimization as currently described is unsafe. Consider the following example:

    void f() {
        X x;
        try {
            throw x;
        } catch (...) {
        }
        // x may have been moved from but can still be accessed here
    }

When the operation is a throw, as opposed to a return, there must be a restriction that the object potentially being moved be defined within the innermost enclosing try block.

History
Date User Action Args
2010-03-29 00:00:00adminsetmessages: + msg2721
2010-03-29 00:00:00adminsetstatus: ready -> cd2
2009-11-08 00:00:00adminsetmessages: + msg2368
2009-11-08 00:00:00adminsetstatus: open -> ready
2009-08-03 00:00:00adminsetmessages: + msg2221
2009-05-06 00:00:00admincreate