Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think this is because `mutable` qualifies the call operator of the lambda (like a reverse const qualifier) so by-value captures are effectively const during the call unless the lambda is marked `mutable`. References themselves are always const, but the referenced object may be modified through the reference depending on its constness even though the lambda is not `mutable`.

Is there a way to force capture by const-reference by the way?



  int main() {
     int x = 0;
     [&x] { x= 1;}(); // works
     [&x=std::as_const(x)] { x= 1;}(); // error: assignment of read-only reference 'x'
   }
Not very pretty, but it works.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: