Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
Why can I not mutate a const ref captured by value?

Take a look at this code snippet:

int x = 3;
const int &y= x;
auto f = [y]() mutable{
  y = 0;
};

clangs gives error:

> clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:13:7: error: cannot assign to a variable captured by copy in a non-mutable
      lambda
    y = 0;
    ~ ^
1 error generated.

It works if capture by [y=y].

Why is this behavior? Is it that compiler deduct type as const int? If so why clang gives error about non-mutable lambda?

[update]
Seems the first part of the question already has an answer as pointed out by others: lambda capture by value mutable doesn't work with const &?
clang's error info seems to be a bug which is not discussed in that question. Should I delete the question?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!