samedi 2 janvier 2021

Assign value to a "global" variable from a lambda function

I'm using the boost spirit x3 semantic parser as lambda functions.

I would to like assign the value of string ti an "external" variable, but my vode don't work:

template <typename Iterator>
bool comment(Iterator first, Iterator last)
{
    string varName = ""; //<- this is my "extranal" variable

    auto do_varName = [&](auto& ctx) 
    {
        cout << "VAR name: " << _attr(ctx) << endl; //This work well

        //This don't work; varName is a external variable
        varName = std::string(  _where(ctx).begin(),
                                _where(ctx).end()) ;
    };

The varName assumed the value of all character that are AFTER the expected content. The output of cout is correct. Why?

Aucun commentaire:

Enregistrer un commentaire