https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96065

            Bug ID: 96065
           Summary: Move elision of returned automatic variable doesn't
                    happen the variable is enclosed in a block
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: b7.10110111 at gmail dot com
  Target Milestone: ---

Consider the following code (at Godbolt's: https://gcc.godbolt.org/z/CyqPF9 ):

```
struct A
{
    A();
    A(A&&);
    A(A const&);
    A& operator=(A&&);
    A& operator=(A const&);
};

A getA()
{
    {
        A a;
        return a;
    }
}

int main()
{
    const A a=getA();
}
```

Here we get A::A() call followed by A::A(A&&) call. If we remove the inner
braces in getA(), move elision happens, so only A::A() is called. I'd expect
that without removal of braces move elision would also happen.

This problem of missing move elision also affects the case when the block
belongs to an if statement. Same pattern happens with copy elision if we
comment out the move constructor.

For comparison, MSVC 19.24 (with /O2 flag) and Clang 10.0 (by default) both
elide the move.

Reply via email to