This code appears to work, but generates some compilation warnings. One specifically regarding rd() being uninitialized, but it's my understanding I'm initializing it. Does anyone know whats wrong and how to eliminate the warnings?
#include <iostream>
#include <random>
#include <map>
#include <functional>
using namespace std;
class rand_int {
public:
rand_int(int lo, int hi) : p{lo,hi} {}
int operator()() const { return r(); }
private:
uniform_int_distribution<>::param_type p;
function<int()> r = bind(uniform_int_distribution<>{p},
default_random_engine{rd()});
random_device rd;
};
int main()
{
map<int,int> m;
for (int i=0; i < 100; ++i) {
m[rand_int{0,9}()]++;
}
for (map<int,int>::iterator it = m.begin();
it != m.end(); ++it)
cout << it->first << ", " << it->second << '\n';
return 0;
}
Compilation:
clang++ -Wall -std=c++11 -pedantic test252.cc && ./a.out
test252.cc:14:31: warning: field 'rd' is uninitialized when used here
[-Wuninitialized]
default_random_engine{rd()});
^
test252.cc:9:5: note: during field initialization in this constructor
rand_int(int lo, int hi) : p{lo,hi} {}
^
1 warning generated.
0, 7
1, 9
2, 11
3, 13
4, 10
5, 8
6, 10
7, 13
8, 7
9, 12
Aucun commentaire:
Enregistrer un commentaire