dimanche 13 septembre 2020

Unexpected behavior return a value from a std::bitset inside a lambda function

I am trying to use the value from a std::bitset inside of a lambda function but I am experiencing unexpected behavior:

int main()
{
    auto lambda = [](unsigned int param)
    {   
        std::bitset<10> bits(param);
        std::cout << "inside lambda bits[8] = " << bits[8] << std::endl; 
        return bits[8];
    };

    // 0x11c is 1_0001_1100 in binary
    auto result = lambda(0x11c);
    std::cout << "result = " << result << std::endl;
}

I would expect the value of result to be one since result is of type bool which can be copied from the local stack but instead the output is the following:

inside lambda bits[8] = 1
result = 0

What am I overlooking here?

Aucun commentaire:

Enregistrer un commentaire