了解随机数生成器调用中的C 参数

Understanding C++ Parameters in random number generator call

本文关键字:参数 随机数生成器 调用 了解      更新时间:2023-10-16

我正在经过一本名为掌握C 多线程的书中的一些示例,并且我遇到了一些我不完全理解的代码。

在此函数中

int randGen(const int& min, const int& max){
    static thread_local mt19937 generator(hash<thread::id>() (this_thread::get_id()));
    uniform_int_distribution<int> distribution(min, max);
    return distribution(generator);
}

我不明白的代码是发电机函数调用

中的参数
hash<thread::id>() (this_thread::get_id())

hash<thread::id>()是从 this_thread::get_id()中获取返回值的功能吗?

任何帮助将不胜感激,或者如果我需要提供更多信息。请大喊。

使用hash<thread::id>()创建std::hash类模板的对象

然后,您将该对象称为operator()函数,将this_thread::get_id()传递为参数。


如果我们将其拆分,则可能更容易理解:

hash<thread::id> my_hash;  // Create object
my_hash(this_thread::get_id());  // Use the function call operator

最后一个使用函数调用操作员,等于

my_hash.operator()(this_thread::get_id());  // Use the function call operator

然后将函数调用操作员的结果用作generator对象的构造函数的参数。