[clang] [Clang] correct error message when assigning to const reference captured in lambda (PR #105647)

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 22 13:31:46 PDT 2024


================
@@ -189,6 +189,11 @@ namespace ModifyingCapture {
     [=] {
       n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
     };
+    const int cn = 0;
+    // cxx03-cxx11-warning at +1 {{initialized lambda captures are a C++14 extension}}
+    [&cnr = cn]{ // expected-note {{variable 'cnr' declared const here}}
----------------
nfrmtk wrote:

Hello.
I've thought of this too.
The change shouldn't be limited to lambdas, but to diagnosing attempts to mutate objects behind const references in general. 
I think, the most precise (if this word makes any sense in the context) solution is to point to the first place where user added const to type. For example
```
int a = 5;
const auto& ar = a;
auto& arr = ar;
auto& arrr = arr;
arrr = 2;
``` 
Should make note pointing to second line([currently](https://godbolt.org/z/7PWd7xbh1) points to fifth).
But I'm not sure if this is common case and will be helpful to the final user.

So, I'm confused and need some help.

https://github.com/llvm/llvm-project/pull/105647


More information about the cfe-commits mailing list