https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92559
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> --- I don't think this can ever be optimized. Mainly because there are copies happening due to passing via value and returning by value. If I change it to foo to MyMap &foo(MyMap &m), I get the behavior you want.