为什么mt19937 m(random_device()())无法编译?

ggr*_*grr 0 c++ syntax compiler-errors

我想创建一个函数,它根据元素的值随机返回向量索引作为概率:

float getRandomIndex(const vector<float>& v){
    random_device r;
    mt19937 m(r());
    return discrete_distribution<>(v.begin(),v.end())(m);
}

int main(){
    for(int i=0;i<10;i++){
        vector<float> a{0.9,0.1};
        cout << getRandomIndex(a) << endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我想减少行号,所以尝试将函数重写为:

float getRandomIndex(const vector<float>& v){
    mt19937 m(random_device()());
    return discrete_distribution<>(v.begin(),v.end())(m);
}
Run Code Online (Sandbox Code Playgroud)

但编译失败:

error: function cannot return function type 'std::__1::random_device ()'
mt19937 m(random_device()());
                       ^
warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
mt19937 m(random_device()());
         ^~~~~~~~~~~~~~~~~~~
note: add a pair of parentheses to declare a variable
mt19937 m(random_device()());
          ^
          (
Run Code Online (Sandbox Code Playgroud)

生成1个警告和1个错误.

但我会尝试

random_device r;
cout << r() << endl;
Run Code Online (Sandbox Code Playgroud)

cout << random_device()() << endl;
Run Code Online (Sandbox Code Playgroud)

它们似乎具有相同的功能,为什么我不能将变量r替换为mt19937中立即创建的random_device?

Wea*_*ish 5

最让人头疼的解析再次罢工!在这种情况下,看起来random_device()()部件被解析为返回函数返回a的函数random_device.在这种情况下,代码可以创建临时变量,也可以声明函数.编译器被迫假设它是一个函数声明.

您可以使用大括号或额外括号来修复它.

m(random_device{}()) //uniform initialization syntax, will work
m((random_device()())) //extra parenthesis, also will work
Run Code Online (Sandbox Code Playgroud)