http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53637

             Bug #: 53637
           Summary: NRVO not applied in branches when it could be
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: ken...@google.com


In the example below it should be possible to apply NRVO.  This situation is
similar to, but more complicated than, the one reported in PR51571.

Note that while this is "just a missed optimization", NRVO can affect the way
interfaces are designed, so aggressively covering all possible cases seems more
important than with most optimizations.

$ cat nrvo.cc 
class Foo {
public:
  Foo() {}

  // Declare but don't define so that if NRVO doesn't kick in we
  // get a linker error.
  Foo(const Foo& other);
};

Foo bar(int i) {
  if (i > 1) {
    Foo result;
    return result;
  } else {
    Foo result;
    return result;
  }
}

int main(int argc, char* argv[]) {
  Foo f = bar(argc);
}

$ g++-4.7 -O2 nrvo.cc
/tmp/ccRlHce1.o: In function `bar(int)':
nrvo.cc:(.text+0xe): undefined reference to `Foo::Foo(Foo const&)'
collect2: error: ld returned 1 exit status
$ g++-4.7 --version
g++-4.7 (Ubuntu/Linaro 4.7.0-7ubuntu3) 4.7.0
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Reply via email to