fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. class Committer
  5. {
  6. public:
  7.  
  8. template <typename T>
  9. void mark_var_change(T *var)
  10. {
  11. mPointer = var;
  12. }
  13.  
  14. template <typename T>
  15. void commit_change(T new_value)
  16. {
  17. if (mPointer == nullptr) {
  18. throw std::runtime_error("nullptr was stocked");
  19. }
  20. *reinterpret_cast<T*>(mPointer) = new_value;
  21. }
  22.  
  23. private:
  24. void* mPointer = nullptr;
  25. };
  26.  
  27.  
  28. int main() {
  29. Committer c;
  30. int i;
  31. float f;
  32.  
  33. c.mark_var_change(&i);
  34. c.commit_change(42);
  35.  
  36. c.mark_var_change(&f);
  37. c.commit_change(4.2f);
  38.  
  39. std::cout << i << " " << f << std::endl;
  40. }
  41.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
42 4.2