Ilya_konstantinov

Untitled

Oct 22nd, 2025 (edited)
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. #include "func.h"
  5.  
  6. void NoRefFoo(Counter x) {
  7.     std::cout << "No ref func: " << x << '\n';
  8. }
  9.  
  10. void RefFoo(const Counter& x) {
  11.     std::cout << "Ref func: " << x << '\n';
  12. }
  13.  
  14. class ReproducibleRNG {
  15. private:
  16.     unsigned long seed_;
  17.    
  18. public:
  19.     ReproducibleRNG(unsigned long seed = 1) : seed_(seed) {}
  20.    
  21.     void set_seed(unsigned long seed) { seed_ = seed; }
  22.    
  23.     long operator()() {
  24.         seed_ = (seed_ * 1103515245 + 12345) & 0x7FFFFFFF;
  25.         return seed_;
  26.     }
  27.    
  28.     long operator()(long min, long max) {
  29.         return min + (operator()() % (max - min + 1));
  30.     }
  31. };
  32.  
  33. int main() {
  34.     static_assert(sizeof(Counter) <= 8);
  35.     int x = 0;
  36.     Counter w(x);
  37.     int n; std::cin >> n;
  38.     ReproducibleRNG rnd(n);
  39.     static_assert(!std::is_invocable_v<decltype(NoRefFoo), Counter>);
  40.    
  41.     static_assert(std::is_invocable_v<decltype(RefFoo), Counter>);
  42.    
  43.     static_assert(std::is_invocable_v<decltype(RefFoo), const Counter&>);
  44.    
  45.     std::cout << "All static assertions passed!\n";
  46.     RefFoo(w);
  47.     x = 19;
  48.     int cnt;
  49.     do {
  50.         cnt = rnd(1, 20);
  51.     } while(cnt == 0);
  52.     for (size_t i = 0; i < cnt; ++i) {
  53.         std::cout << w << '\n';
  54.     }
  55.     std::cout << w << '\n';
  56.     x = 38;
  57.     std::cout << w << '\n';    
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment