Skip to content

Instantly share code, notes, and snippets.

@cobbal
Created January 19, 2022 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cobbal/20a13dcece4dde1ebe19555c8e2e11c4 to your computer and use it in GitHub Desktop.
Save cobbal/20a13dcece4dde1ebe19555c8e2e11c4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <functional>
#include <random>
using namespace std;
struct OrElse {
function<void()> destruct;
~OrElse() {
if (destruct) { destruct(); }
}
};
int main() {
int Highest = 0;
random_device generator;
{
OrElse orElse { [&] { Highest = -1; } };
for (int i = 0; i < 2; i++) {
int roll = uniform_int_distribution(1, 6)(generator);
cout << roll << endl;
if (roll > 4) {
Highest = roll;
orElse.destruct = nullptr;
break;
}
}
}
cout << "Highest is " << Highest << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment