fork download
  1. #include <iostream>
  2. class Foo {
  3. public:
  4. Foo()
  5. {
  6. throw std::runtime_error("Oops!");
  7. }
  8. };
  9. struct Arena {
  10. bool touched = false;
  11. };
  12. void* operator new(std::size_t, Arena& arena)
  13. {
  14. std::cout << "new has called.\n";
  15. return ::operator new(sizeof(Foo));
  16. }
  17. void operator delete(void*, Arena& arena)
  18. {
  19. std::cout << "delete has called.\n";
  20. arena.touched = true;
  21. }
  22. int main(int argc, char** argv)
  23. {
  24. Arena arena;
  25. try {
  26. new(arena) Foo;
  27. }
  28. catch(...) {
  29. std::cout << "exception was thrown.\n";
  30. }
  31. // This differs for different type of builds.
  32. std::cout << std::boolalpha << "arena.touched == " << arena.touched << '\n';
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 4456KB
stdin
Standard input is empty
stdout
new has called.
delete has called.
exception was thrown.
arena.touched == true