Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uniform_real_distribution operator () compilation errors

Tags:

c++

g++

ubuntu

I've written this code:

std::pair<std::weak_ptr<Node>, size_t> NodeSelector::rouletteWheel() const {
    const std::uniform_real_distribution<long double> urd;
    size_t i = 0;
    for(auto rnd = urd(shared_random_engine); rnd >= 0; ++i) {
        rnd -= getNormValue(i);
    }
    return std::make_pair(entries_[i - 1], i - 1);
}

Where "shared_random_engine" is initialized in this way (in a separate .h file)

std::minstd_rand shared_random_engine(static_cast<unsigned int>(42));

As long as I compile it on Windows, everything works perfectly, but when I try to compile on Ubuntu 18.04.2 with g++ 7.3.0, I get this error:

montecarlo.cpp: In member function ‘std::pair<std::weak_ptr<MonteCarloNode>, long unsigned int> NodeSelector::rouletteWheel() const’:
montecarlo.cpp:728:41: error: no match for call to ‘(const std::uniform_real_distribution<long double>) (std::minstd_rand&)’
  for(auto rnd = urd(shared_random_engine); rnd >= 0; ++i) {
                                         ^
In file included from /usr/include/c++/7/random:49:0,
                 from selector.h:15,
                 from montecarlo.h:13,
                 from montecarlo.cpp:7:
/usr/include/c++/7/bits/random.h:1813:2: note: candidate: std::uniform_real_distribution<_RealType>::result_type std::uniform_real_distribution<_RealType>::operator()(_UniformRandomNumberGenerator&) [with _UniformRandomNumberGenerator = std::linear_congruential_engine<long unsigned int, 48271, 0, 2147483647>; _RealType = long double; std::uniform_real_distribution<_RealType>::result_type = long double] <near match>
  operator()(_UniformRandomNumberGenerator& __urng)
  ^~~~~~~~
/usr/include/c++/7/bits/random.h:1813:2: note:   passing ‘const std::uniform_real_distribution<long double>*’ as ‘this’ argument discards qualifiers
/usr/include/c++/7/bits/random.h:1818:2: note: candidate: template<class _UniformRandomNumberGenerator> std::uniform_real_distribution<_RealType>::result_type std::uniform_real_distribution<_RealType>::operator()(_UniformRandomNumberGenerator&, const std::uniform_real_distribution<_RealType>::param_type&) [with _UniformRandomNumberGenerator = _UniformRandomNumberGenerator; _RealType = long double]
  operator()(_UniformRandomNumberGenerator& __urng,
  ^~~~~~~~
/usr/include/c++/7/bits/random.h:1818:2: note:   template argument deduction/substitution failed:
montecarlo.cpp:728:41: note:   candidate expects 2 arguments, 1 provided
  for(auto rnd = urd(shared_random_engine); rnd >= 0; ++i) {
                                         ^

Since the last lines of the errors tell me that 2 arguments are expected, I changed the code in the following way:

for(auto rnd = urd(shared_random_engine, urd.param())

On Windows, it still works fine, but on Ubuntu I get this error, now telling me to pass just 1 argument:

montecarlo.cpp: In member function ‘std::pair<std::weak_ptr<MonteCarloNode>, long unsigned int> NodeSelector::rouletteWheel() const’:
montecarlo.cpp:728:54: error: no match for call to ‘(const std::uniform_real_distribution<long double>) (std::minstd_rand&, std::uniform_real_distribution<long double>::param_type)’
  for(auto rnd = urd(shared_random_engine, urd.param()); rnd >= 0; ++i) {
                                                      ^
In file included from /usr/include/c++/7/random:49:0,
                 from selector.h:15,
                 from montecarlo.h:13,
                 from montecarlo.cpp:7:
/usr/include/c++/7/bits/random.h:1813:2: note: candidate: template<class _UniformRandomNumberGenerator> std::uniform_real_distribution<_RealType>::result_type std::uniform_real_distribution<_RealType>::operator()(_UniformRandomNumberGenerator&) [with _UniformRandomNumberGenerator = _UniformRandomNumberGenerator; _RealType = long double]
  operator()(_UniformRandomNumberGenerator& __urng)
  ^~~~~~~~
/usr/include/c++/7/bits/random.h:1813:2: note:   template argument deduction/substitution failed:
montecarlo.cpp:728:54: note:   candidate expects 1 argument, 2 provided
  for(auto rnd = urd(shared_random_engine, urd.param()); rnd >= 0; ++i) {
                                                      ^
In file included from /usr/include/c++/7/random:49:0,
                 from selector.h:15,
                 from montecarlo.h:13,
                 from montecarlo.cpp:7:
/usr/include/c++/7/bits/random.h:1818:2: note: candidate: std::uniform_real_distribution<_RealType>::result_type std::uniform_real_distribution<_RealType>::operator()(_UniformRandomNumberGenerator&, const std::uniform_real_distribution<_RealType>::param_type&) [with _UniformRandomNumberGenerator = std::linear_congruential_engine<long unsigned int, 48271, 0, 2147483647>; _RealType = long double; std::uniform_real_distribution<_RealType>::result_type = long double] <near match>
  operator()(_UniformRandomNumberGenerator& __urng,
  ^~~~~~~~
/usr/include/c++/7/bits/random.h:1818:2: note:   passing ‘const std::uniform_real_distribution<long double>*’ as ‘this’ argument discards qualifiers

Does anybody know what's the problem here?

like image 448
Mdp11 Avatar asked Mar 20 '19 14:03

Mdp11


1 Answers

std::uniform_real_distrubution::operator() is not a constant member function, therefore cannot be invoked for constant instances: [rand.dist.uni.real].


Why does it work on Windows? Microsoft's operator() is likely const, IIRC implementations are allowed to strengthen const specification for member functions: http://eel.is/c++draft/member.functions#2.

like image 115
Daniel Langr Avatar answered Oct 11 '22 09:10

Daniel Langr