Skip to content

random usage in cpp2? #1278

Answered by DyXel
feature-engineer asked this question in Q&A
Sep 16, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

The issue you are having is due to the fact that Cpp2 automatically moves from definite last use:

random_between_0_and_1: () -> double = {
    rd: std::random_device = ();
    gen: std::mt19937 = (rd());
    dist: std::uniform_real_distribution<double> = (0.0, 1.0);
    
    return dist(gen); // <-- definite last use of `gen`, so its cpp2::move'd.
}

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:

random_between_0_and_1: () -> double = {
    rd: std::random_device = ();
    gen: std::mt19937 = (rd());
    dist: std::uniform_real_distribution<double> = (0.0, 1.0);
   …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@feature-engineer
Comment options

@DyXel
Comment options

Answer selected by feature-engineer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants