当我查看分布的文档时,它似乎没有公开在运行时更改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);
}是否有一种记录在案的方法来做到这一点,如果没有,这两种解决方案中的任何一种是否安全使用?
发布于 2017-04-03 22:19:59
只需为旧实例分配一个新生成器:
std::exponential_distribution<double> intervalGenerator(5);
intervalGenerator = std::exponential_distribution<double>(7);便携,易读,明显正确。
另外,
intervalGenerator.param(std::exponential_distribution<double>::param_type(7));正如26.5.1.6/9在N3337和N4141中所描述的那样是安全的,所以您也可以使用它。但是,对于第一个变体,从一开始就不会出现可移植性问题。
https://stackoverflow.com/questions/43195248
复制相似问题