如何找出某个标准C++函数的实现细节?
How to find out the implementation details for a certain standard C++ function?
让我们以 std::normal_distribution
为例。
我知道有一些算法可以从正态分布中抽样(参考:Wikipedia). And I know that the standard specification usually leaves it to the implementation to choose the algorithm (reference: SO)。
但是,有时需要指定在后台使用的算法。如何查明实施细节?
(我承认我对这个世界上存在的 C++ 标准库的不同实现知之甚少。大多数情况下,我使用的是 XCode/clang、gcc 和 MSVC 附带的那些。 )
有时实用程序的行为是由标准明确定义的,有时不是。
- 看标准;如果找到,它是严格符合和便携的。哟嘿!
- 如果未指定或明确实现定义,请查看您选择的标准库实现。源码会有解释。
不幸的是,它是实现定义的并且不可移植。如果由 POSIX 或类似的东西指定,再次 yoohay,但仅适用于 POSIX-conforming 或 "something similar"-conforming 平台。
这是一个例子:
C++14 标准草案 N4296 在§26.5.8.5.1 中说:
A normal_distribution
random number distribution produces random
numbers x
distributed according to the probability density function
The distribution parameters µ
and σ
are also known as this distribution's mean and standard deviation.
我不知道 PRNG,所以我无法向您解释这个公式,但我认为这就是您要找的东西。
你有一个函数(更具体地说:"probability density function")使用正态分布计算随机数。整个算法围绕此构建,可以在相应的标准库实现中找到。
我不得不像这样写一个 Wrapper class:
struct Generator{
Generator() : val(0), count(0) {}
Generator(std::mt19937&& aGen) : val(0), count(0), theGen(aGen) {}
long long int operator()(void){
val = theGen();
std::cout << val << " " << count << std::endl;
++count;
return val;
}
long long int max(){return theGen.max();};
long long int min(){return theGen.min();};
long long int val;
size_t count;
std::mt19937 theGen;
};
反省我的编译器如何实现 normal_distribution。
为了看到它的实际效果,您必须按照以下几行写一些东西:
std::normal_distribution<> dis(1.0, 2.0);
Generator gen(std::mt19937 (42));
dis(gen);
迭代调用 dis(gen);
可能具有指导意义
让我们以 std::normal_distribution
为例。
我知道有一些算法可以从正态分布中抽样(参考:Wikipedia). And I know that the standard specification usually leaves it to the implementation to choose the algorithm (reference: SO)。
但是,有时需要指定在后台使用的算法。如何查明实施细节?
(我承认我对这个世界上存在的 C++ 标准库的不同实现知之甚少。大多数情况下,我使用的是 XCode/clang、gcc 和 MSVC 附带的那些。 )
有时实用程序的行为是由标准明确定义的,有时不是。
- 看标准;如果找到,它是严格符合和便携的。哟嘿!
- 如果未指定或明确实现定义,请查看您选择的标准库实现。源码会有解释。
不幸的是,它是实现定义的并且不可移植。如果由 POSIX 或类似的东西指定,再次 yoohay,但仅适用于 POSIX-conforming 或 "something similar"-conforming 平台。
这是一个例子:
C++14 标准草案 N4296 在§26.5.8.5.1 中说:
A
normal_distribution
random number distribution produces random numbersx
distributed according to the probability density function
The distribution parameters
µ
andσ
are also known as this distribution's mean and standard deviation.
我不知道 PRNG,所以我无法向您解释这个公式,但我认为这就是您要找的东西。
你有一个函数(更具体地说:"probability density function")使用正态分布计算随机数。整个算法围绕此构建,可以在相应的标准库实现中找到。
我不得不像这样写一个 Wrapper class:
struct Generator{
Generator() : val(0), count(0) {}
Generator(std::mt19937&& aGen) : val(0), count(0), theGen(aGen) {}
long long int operator()(void){
val = theGen();
std::cout << val << " " << count << std::endl;
++count;
return val;
}
long long int max(){return theGen.max();};
long long int min(){return theGen.min();};
long long int val;
size_t count;
std::mt19937 theGen;
};
反省我的编译器如何实现 normal_distribution。
为了看到它的实际效果,您必须按照以下几行写一些东西:
std::normal_distribution<> dis(1.0, 2.0);
Generator gen(std::mt19937 (42));
dis(gen);
迭代调用 dis(gen);
可能具有指导意义