random usage in cpp2? #1278
-
Hi, I'm trying to convert this code to cpp2: auto random_between_0_and_1() -> double {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dist(0.0, 1.0);
return dist(gen);
} I encounter an error for the return dist(gen) line for the simpler case where I don't use static.
Doesn't compile due to the following error: no match for call to ‘(std::uniform_real_distribution) (std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>)’ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The issue you are having is due to the fact that Cpp2 automatically moves from definite last use:
Usually, what you do in these cases is explicitly discard the new state of the object that was mutated. In order to do that, you'd need to create an intermediary variable:
However this is annoying since you introduce a temporary on top of having the discard explicitly. Instead, what you can do is use a named return value, which is only 1 extra line total (the explicit discard):
|
Beta Was this translation helpful? Give feedback.
The issue you are having is due to the fact that Cpp2 automatically moves from definite last use:
Usually, what you do in these cases is explicitly discard the new state of the object that was mutated. In order to do that, you'd need to create an intermediary variable: