[Bug c++/98938] New: throw calls move constructor
njormrod at fb dot com
gcc-bugzilla@gcc.gnu.org
Tue Feb 2 18:47:09 GMT 2021
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;
}
```
More information about the Gcc-bugs
mailing list