https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98938

            Bug ID: 98938
           Summary: throw calls move constructor
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: njormrod at fb dot com
  Target Milestone: ---

Throwing invokes the move constructor instead of the copy constructor.

Demonstration of move-constructor in godbolt: https://gcc.godbolt.org/z/zhjv5z
Demonstration of copy-constructor using clang: https://gcc.godbolt.org/z/E3MEef

>From 14.2 Throwing an exception [except.throw]: "Throwing an exception
copy-initializes (9.4, 11.4.5.3) a temporary object, called the exception
object."

Code (as used in godbolt):

```
#include <exception>

struct A : std::exception {
    A(int* q) : p(q) {}
    A(const A& a) : p(a.p) { *p = 12345; }
    A(A&& a) : p(a.p) { *p = 56789; }

    int* p;
};

int foo() {
    int i = 10;
    A a(&i);
    try {
        throw a;
    } catch (A&) {}
    return i;
}
```

Reply via email to