https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113083
--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> --- What about: 2024-02-22 Jakub Jelinek <ja...@redhat.com> PR c++/113083 * cp-gimplify.cc (cp_fold): For targetm.cxx.cdtor_returns_this () wrap r into a COMPOUND_EXPR and return folded CALL_EXPR_ARG (x, 0). * g++.dg/cpp0x/constexpr-113083.C: New test. --- gcc/cp/cp-gimplify.cc.jj 2024-02-22 21:45:09.663430066 +0100 +++ gcc/cp/cp-gimplify.cc 2024-02-22 22:16:41.816591451 +0100 @@ -3415,6 +3415,10 @@ cp_fold (tree x, fold_flags_t flags) tree s = build_fold_indirect_ref_loc (loc, CALL_EXPR_ARG (x, 0)); r = cp_build_init_expr (s, r); + if (targetm.cxx.cdtor_returns_this ()) + r = build2 (COMPOUND_EXPR, TREE_TYPE (x), r, + fold_convert (TREE_TYPE (x), + CALL_EXPR_ARG (x, 0))); } x = r; break; --- gcc/testsuite/g++.dg/cpp0x/constexpr-113083.C.jj 2024-01-13 00:05:00.077372302 +0100 +++ gcc/testsuite/g++.dg/cpp0x/constexpr-113083.C 2024-02-22 22:20:20.622618992 +0100 @@ -0,0 +1,16 @@ +// PR c++/113083 +// { dg-do compile { target c++11 } } +// { dg-options "-Os" } + +struct A { constexpr A (); }; + +void +foo () +{ + A b; +} + +constexpr +A::A () +{ +} Though, perhaps we need cp_save_expr in that case to make sure if there were side-effects in CALL_EXPR (x, 0) that we wouldn't evaluate them twice.