samedi 17 décembre 2022

How does the `auto` keyword work in lambda functions?

I am new to templates and lambdas function and I am trying to understand what's happening in this piece of code:

function<void (const int &)> foo = [&](auto x) {
    x = 42;
}

Here, I assume the compiler is able to tell from the auto keyword that x has type const int &. Therefore, the compiler should not allow the assignment x = 42.

However, this code compiles without error, which confuses me. Why does the compiler allow this? What did the compiler actually deduced for the auto keyword?

Aucun commentaire:

Enregistrer un commentaire