dimanche 27 août 2017

Can not capture a random distribution and generator by value in lambda?

The following code works fine

#include <iostream>
#include <random>

int main() {
    std::default_random_engine generator;
    std::normal_distribution<double> distribution(5.0, 2.0);
    std::vector<double> v(100);

    std::generate(v.begin(), v.end(), [&] () { return distribution(generator); });

    return 0;
}

However, changing the capture list of the lambda to [=] or [&, distribution] or [&, generator] causes

rand.cpp:9:59: error: no matching function for call to object of type 'const std::normal_distribution<double>'
error: assigning to 'double' from incompatible type 'void'

Are there some kinds of objects can not be captured by value in a lambda ?

Aucun commentaire:

Enregistrer un commentaire