Advertisement
cepxuozab

LazyValue

Jan 15th, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. struct Counter {
  2.         static size_t constructed;
  3.         Counter() { ++constructed; }
  4.         Counter(const Counter&) { ++constructed; }
  5.         Counter(Counter&&) = default;
  6.         Counter& operator=(const Counter&) { ++constructed; return *this; }
  7.         Counter& operator=(Counter&&) = default;
  8.         ~Counter() = default;
  9.         size_t value = 0;
  10.     };
  11.     size_t Counter::constructed = 0;
  12.  
  13. void Test(){
  14. Counter::constructed = 0;
  15. LazyValue<Counter> val([]() { return Counter(); });
  16. assert(Counter::constructed == 0u);
  17. assert(!val.HasValue());
  18.  val.Get();
  19. assert(Counter::constructed == 1u);
  20. assert(val.HasValue());
  21. Counter counter = val.Get();
  22. assert(Counter::constructed == 2u);
  23. essert(counter.value == 0);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement