https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87514
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Jonathan Wakely from comment #0) > It's possible this can already be devirtualized by the compiler, but only > when optimising. If it isn't, then this might help: > > @@ -653,9 +652,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > typedef _Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp> _Sp_cp_type; > typename _Sp_cp_type::__allocator_type __a2(__a); > auto __guard = std::__allocate_guarded(__a2); > - _Sp_cp_type* __mem = __guard.get(); > - ::new (__mem) _Sp_cp_type(__a, std::forward<_Args>(__args)...); > - _M_pi = __mem; > + _M_pi = ::new (__guard.get()) > + _Sp_cp_type(__a, std::forward<_Args>(__args)...); > __guard = nullptr; > } It looks like it already gets devirtualized at -O2 and above, and the change above doesn't change anything for -O1.