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

            Bug ID: 116439
           Summary: [14/15 Regression] decltype(auto) in return type of
                    lambda uses the type of the outer scope, not the
                    capture
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: valentin at tolmer dot fr
  Target Milestone: ---

$ cat source.cpp
struct S {
  S() = default;
  S(const S&) = delete;
};

int main() {
  S source;
  [&] -> decltype(auto) {
    return source;
  }();
}
$ g++ -std=c++23 source.cpp
source.cpp: In lambda function:
source.cpp:9:12: error: use of deleted function 'S::S(const S&)'
    9 |     return source;
      |            ^~~~~~
source.cpp:3:3: note: declared here
    3 |   S(const S&) = delete;
      |   ^
source.cpp:9:12: note: use '-fdiagnostics-all-candidates' to display considered
candidates
    9 |     return source;
      |            ^~~~~~
Compiler returned: 1

AFAIU, starting from 14.1 the decltype(auto) deduces the type from the `S
source;`, the outer variable, rather than the implicit by-ref capture of the
same name. Writing `return (source);` correctly deduces the reference.
Introducing an explicitly named capture `[&source = source]` correctly deduces
the reference.
GCC 13.3 and below returns a reference.

Reply via email to