dimanche 27 août 2017

Are all

Notice that std::normal_distribution::operator() is not const, nor does it behave in a const way. (Some other distributions have () operators that behave in a const way, but are also not defined to be const).

Given that std::normal_distribution::operator() is not const, is it still safe to use the same normal_distribution object across multiple threads? Is it safe for all distributions in the random header?

Edit: That is, the following member function throws an error due to the function being const but using the operator(), which can change d. Is it always safe to fix this by declaring d to be mutable?

class MyClass
{
public:
    MyClass::MyClass(double mu, double sigma)
    {
        d = normal_distribution<double>(mu, sigma);
    }

    double MyClass::foo(std::mt19937_64 & generator) const
    {
        return d(generator);
    }
private:
    std::normal_distribution<double> d;
}




Aucun commentaire:

Enregistrer un commentaire