fork download
  1. #include <iostream>
  2. #include <random>
  3. #include <vector>
  4.  
  5.  
  6. int main() {
  7. std::random_device rd;
  8. std::mt19937 gen(rd());
  9. std::uniform_real_distribution<> dis(0,1);
  10.  
  11. // Use fifty-fifty coin tosses to set some booleans.
  12. // In actual code, these will be set by some condition.
  13. bool set_a = dis(gen) < 0.5;
  14. bool success = dis(gen) < 0.5;
  15. bool get_last = dis(gen) < 0.5;
  16.  
  17. // Don't ever do this!
  18. int a = 0, b = 0;
  19. (set_a ? a : b) = 42;
  20. std::cout << "a=" << a << " b=" << b << '\n';
  21.  
  22. // On the other hand, there's nothing at all wrong with this.
  23. std::cout << "Status = " << (success ? "Success" : "Failure") << '\n';
  24.  
  25. // There's nothing at all wrong with this, either.
  26. std::vector<int> buffer(42);
  27. int nlines = get_last ? 1 : buffer.size();
  28. std::cout << "nlines = " << nlines << '\n';
  29.  
  30. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
a=42  b=0
Status = Success
nlines = 1