vendredi 23 novembre 2018

C++11 why is the lambda's operator () implicitly const?

I have a small "lambda expression" in the below function:

int main()
{
    int x = 10;
    auto lambda = [=] () { return x + 3; };
}

Below is the "anonymous closure class" generated for the above lambda expression.

int main()
{
    int x = 10;

    class __lambda_3_19
    {
        public: inline /*constexpr */ int operator()() const
        {
            return x + 3;
        }

        private:
            int x;

        public: __lambda_3_19(int _x) : x{_x}
          {}

    };

    __lambda_3_19 lambda = __lambda_3_19{x};
}

The closure class "operator()" generated by the compiler is implicitly const. Why does the Standard committee made it const by default?

Aucun commentaire:

Enregistrer un commentaire