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

            Bug ID: 117900
           Summary: Spurious call to copy constructor when
                    list-initializing a const reference
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carsonradtke at microsoft dot com
  Target Milestone: ---

Consider the following example which is accepted by Clang and MSVC:
```
// https://godbolt.org/z/MT4EdzreK
// -std=c++20

struct S {
    S(const S &) = delete;
};

using Sref = const S&;

void foo(S & s)
{
    (void)Sref{s};
}
```

GCC rejects this snippet with the following diagnostic:
```
<source>: In function 'void foo(S&)':
<source>:9:16: error: use of deleted function 'S::S(const S&)'
    9 |     (void)Sref{s};
      |                ^
<source>:2:5: note: declared here
    2 |     S(const S &) = delete;
      |     ^
Compiler returned: 1
```

The call to `Sref{s}` should not invoke the copy constructor because the
reference object should bind directly to the initializer, s (per
https://eel.is/c++draft/dcl.init.ref#5.1).

Some similar issues exist, but I don't think they directly apply here:
 - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90390 - The name, "incorrect
list initialization behavior for references" sounds promising, but the examples
deviate from this snippet.
 - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53931 - Has a similar repro,
but has to do more with casting than with initialization.

Reply via email to