https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87502
--- Comment #6 from M Welinder <terra at gnome dot org> --- > const_cast<std::string&>(s)="some longer string so it needs proper deletion"; Is that really valid? This comes down to whether the temporary object creating with the function call is constant [in which case the above is UB] or not [in which case the above is perfectly fine]. If I am reading http://cpp14.centaur.ath.cx/dcl.init.ref.html right then the object is created with the const/volatile specifiers of the argument type, in this case const. If so, such an object must not be changed. Just in case I am reading it wrong, I still say the generated code is excessive. gcc creates 16 instructions that, among other things, check the _S_force_new flag. Most of those instructions will never be executed in practice. I think the generated code, at least for the temporary-from-small-string case, should simply be movq (%rsp), %rdi leaq 16(%rsp), %rax cmpq %rax, %rdi ;; check if buffer is allocated je .L42 call do_the_complicated_thing .L42: