uniform_real_distribution运算符()编译错误

时间:2019-03-20 14:32:26

标签: c++ ubuntu g++

我已经编写了这段代码:

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);
}

以这种方式初始化“ shared_random_engine”的地方(在单独的.h文件中)

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

只要我在Windows上编译它,一切都可以正常运行,但是当我尝试在带有g ++ 7.3.0的Ubuntu 18.04.2上编译时,出现此错误:

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) {
                                         ^

由于错误的最后几行告诉我期望有2个参数,因此我以以下方式更改了代码:

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

在Windows上,它仍然可以正常工作,但是在Ubuntu上却出现此错误,现在告诉我只传递1个参数:

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

有人知道这是什么问题吗?

1 个答案:

答案 0 :(得分:5)

([^;]*);(?:[^;]*);([^;]*);{1,2}([^;]*);{1,2}(?:.*);不是常量成员函数,因此不能为常量实例[rand.dist.uni.real]调用。


为什么在Windows上可以使用?微软的std::uniform_real_distrubution::operator()很可能是operator(),允许IIRC实现增强成员功能http://eel.is/c++draft/member.functions#2const规范。