http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51571
Guillaume Melquiond changed:
What|Removed |Added
CC||guillaume.melquiond at inria
dot f
||r
--- Comment #3 from Guillaume Melquiond
---
I have recently encountered a similar problem, but in a much more general case.
struct A {
A(int);
A(A const &);
~A();
};
A f(bool b)
{
if (b) return A(0);
A a(1);
return a;
}
All the return statements dominated by variable "a" return "a", so its
construction should happen in-place, hence eliding copy-construction and
destruction. Unfortunately, this is not what happens with g++ 4.8.0.
Interestingly enough, if one uninlines the code by hand, g++ actually generates
the optimal code, so it is possible though cumbersome to work around the missed
optimization:
inline A f2()
{
A a(1);
return a;
}
A f1(bool b)
{
if (b) return A(0);
return f2();
}
produces
A f1(bool) (bool b)
{
:
if (b_2(D) != 0)
goto ;
else
goto ;
:
A::A (_4(D), 0);
goto ;
:
A::A (_4(D), 1);
:
return _4(D);
}