https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102704
Bug ID: 102704 Summary: NRVO for throw expression Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: vanyacpp at gmail dot com Target Milestone: --- Consider this code: struct mytype { mytype(); mytype(mytype const&); mytype(mytype&&); }; void test() { mytype e; throw e; } Currently for function test() GCC generates the following sequence of calls (pseudocode): char e[sizeof(mytype)]; mytype_default_ctor(e); p = __cxa_allocate_exception(); mytype_move_ctor(p, e); __cxa_throw(p); I believe a trick similar to NRVO for returns can be made here. When a variable meets NRVO criteria, compiler can remove the local variable and replace it with a storage allocated by __cxa_allocate_exception. Here what I believe can be generated: p = __cxa_allocate_exception(); mytype_default_ctor(p); __cxa_throw(p);