[Bug c++/58050] New: RVO fails when calling static function through unnamed temporary
scovich at gmail dot com
gcc-bugzilla@gcc.gnu.org
Fri Aug 2 03:08:00 GMT 2013
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58050
Bug ID: 58050
Summary: RVO fails when calling static function through unnamed
temporary
Product: gcc
Version: 4.8.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: scovich at gmail dot com
Return value optimization is not applied when calling a static member function
via an unnamed temporary (value or pointer, it doesn't matter). Calling the
function directly, or through a named value/pointer, works as expected:
// <<<--- bug.cpp --->>>
extern "C" int puts(char const*);
struct B {
~B() { puts("\t~B"); }
};
struct A {
static B make() { return B(); }
} a;
A *ap() { return &a; }
int main () {
puts("b1");
{B b = A::make();}
puts("b2");
{B B = a.make();}
puts("b3");
{B b = ap()->make();}
puts("b4");
{B b = A().make();}
}
// <<<--- end bug.cpp --->>>
Output is (same for both 4.8.1 and 4.6.3):
$ g++ bug.cpp && ./a.out
b1
~B
b2
~B
b3
~B
~B
b4
~B
~B
The workaround is simple enough to apply, if you happen to notice all the extra
object copies being made; I isolated the test case from an app that used 5x
more malloc bandwidth than necessary because a single static function called
the wrong way returned a largish STL object by value.
More information about the Gcc-bugs
mailing list