From: Jason Merrill Date: Wed, 5 Oct 2016 22:59:02 +0000 (-0400) Subject: Implement P0135R1, Guaranteed copy elision. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=bf9b09fbe23d4efb07eec21bbb3f73f1a075cb2b;p=gcc.git Implement P0135R1, Guaranteed copy elision. * cvt.c (ocp_convert): Don't re-copy a TARGET_EXPR in C++17. From-SVN: r240820 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 569d696546d..7bd7db64f4e 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,8 @@ 2016-10-05 Jason Merrill + Implement P0135R1, Guaranteed copy elision. + * cvt.c (ocp_convert): Don't re-copy a TARGET_EXPR in C++17. + PR c++/54293 * call.c (reference_binding): Fix binding to member of temporary. diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c index 2f5f15a2c82..ecc8ef82b2a 100644 --- a/gcc/cp/cvt.c +++ b/gcc/cp/cvt.c @@ -693,8 +693,11 @@ ocp_convert (tree type, tree expr, int convtype, int flags, if (error_operand_p (e)) return error_mark_node; - if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP)) - /* We need a new temporary; don't take this shortcut. */; + if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP) + && !(cxx_dialect >= cxx1z + && TREE_CODE (e) == TARGET_EXPR)) + /* We need a new temporary; don't take this shortcut. But in C++17, don't + force a temporary if we already have one. */; else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e))) { if (same_type_p (type, TREE_TYPE (e))) diff --git a/gcc/testsuite/g++.dg/cpp1z/elide1.C b/gcc/testsuite/g++.dg/cpp1z/elide1.C new file mode 100644 index 00000000000..a0538bbbef8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/elide1.C @@ -0,0 +1,16 @@ +// { dg-options -std=c++1z } + +struct A +{ + A(); + A(const A&) = delete; +}; + +bool b; +A a = A(); +A a1 = b ? A() : A(); +A a2 = (42, A()); + +A f(); +A a3 = f(); +A a4 = b ? A() : f();