Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

321
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda