Issue 180460
Summary Incorrect type deduction in a non-mutable lambda that captures by value
Labels new issue
Assignees
Reporter lakshayg
    The C++23 standard has an example showing how types are deduced in a lambda: https://timsong-cpp.github.io/cppwp/n4950/expr.prim.id.unqual#example-1. Relevant parts of the example are reproduced below.

```cpp
void f() {
 float x;

  [=]() -> decltype((x)) {      // lambda returns float const& because this lambda is not mutable and
                                // x is an lvalue
    decltype(x) y1;             // y1 has type float
 decltype((x)) y2 = y1;      // y2 has type float const&
    return y2;
 };
}
```

This example fails to compile with clang https://godbolt.org/z/zqEsa7T8f.

```
<source>:21:12: error: binding reference of type 'float' to value of type 'const float' drops 'const' qualifier
   21 |     return y2;
      |            ^~
```

Clang replaces `decltype((x))` with `float&` in the _trailing-return-type_. However, in the _compound-statement_, `decltype((x))` is replaced with `const float&`. This causes the compilation to fail.

It seems that clang is still looking at the outside `x` and not the `x` in the closure generated by the lambda when evaluating the `decltype` in the _trailing-return-type_.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to