【发布时间】:2017-08-28 22:42:53
【问题描述】:
当我查看std::exponential_distribution 的文档时,它似乎没有公开在运行时更改 lambda 参数的标准方法。
有一个param 方法,但它采用不透明的成员类型param_type,并且获得这种类型对象的唯一记录方法是不带参数调用param,但这意味着不同必须首先使用该参数创建实例。
下面,我展示了两种未记录的重置 lambda 编译的方法,但我不知道它们是否会在运行时产生正确的行为。
#include <random>
#include <new>
int main(){
std::random_device rd;
std::mt19937 gen(rd());
std::exponential_distribution<double> intervalGenerator(5);
// How do we change lambda after creation?
// Construct a param_type using an undocumented constructor?
intervalGenerator.param(std::exponential_distribution<double>::param_type(7));
// Destroy and recreate the distribution?
intervalGenerator.~exponential_distribution();
new (&intervalGenerator) std::exponential_distribution<double>(9);
}
是否有记录的方法可以做到这一点,如果没有,这两种解决方案中的任何一种都可以安全使用吗?
【问题讨论】:
-
这不是无证的。
param_type始终可以使用与父分布相同的参数构造。 -
@Praetorian,是否有关于其构造函数的文档?我找不到任何东西。
-
param_type可以使用与其分布相同的参数构造这一事实是RandomNumberDistribution概念 (26.5.1.6/9) 的要求之一。
标签: c++ c++11 exponential-distribution