diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 3506b0fcfbb..9fad3cb950b 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -6009,7 +6009,13 @@ build_conditional_expr (const op_location_t &loc, but now we sometimes wrap them in NOP_EXPRs so the test would fail. */ if (CLASS_TYPE_P (TREE_TYPE (result))) - result = get_target_expr (result, complain); + { + result = get_target_expr (result, complain); + /* Tell gimplify_modify_expr_rhs not to strip this in + assignment context: we want both arms to initialize + the same temporary. */ + TARGET_EXPR_NO_ELIDE (result) = true; + } /* If this expression is an rvalue, but might be mistaken for an lvalue, we must add a NON_LVALUE_EXPR. */ result = rvalue (result); diff --git a/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C b/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C new file mode 100644 index 00000000000..b15635853f2 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/cond-temp1.C @@ -0,0 +1,16 @@ +// Test that the ?: only creates one temporary. +// { dg-additional-options "-fdump-tree-gimple" } +// { dg-final { scan-tree-dump-times "struct A" 2 "gimple" } } + +struct A +{ + int i; + A(int); +}; + +bool b; +int main() +{ + A a = 1; + a = b ? A(2) : A(3); +}