lundi 30 juillet 2018

fail to capture random engine by value

I have this code that cannot be compiled by gcc 8, but I cannot understand why.

#include <iostream>
#include <algorithm>
#include <random>
using namespace std;

template<class... T>
void diagnose(T... x);

int main()
{
    auto d = normal_distribution<double>(0.0, 1.0);
    auto g = default_random_engine();
    cout << d(g) << endl;
    auto gen = [=](){
        //diagnose(d, g);
        return d(g);   // ******
    };
    cout << gen() << endl;
}

The error message says (pointing to the line marked by *******):

error: no match for call to ‘(const std::normal_distribution<double>) (const std::linear_congruential_engine<long unsigned int, 16807, 0, 2147483647>&)

The code, however, works if I change the capture to be by reference.

If I uncomment the commented //diagnose line, the error message is like this (need also change return d(g) to return 1.0):

undefined reference to `void diagnose<std::normal_distribution<double>, std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >(std::normal_distribution<double>, std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul>)'

As you can see, in the capture-by-value case, the parameter g is a const reference. But the const does not appear in diagnosis.

Can somebody explain what is going on here?

Aucun commentaire:

Enregistrer un commentaire