Advertisement
den4ik2003

Untitled

Aug 17th, 2022
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. template <typename T>
  2. struct deleter {
  3.   void operator()(T* ptr) {
  4.     delete ptr;
  5.   }
  6. };
  7.  
  8.  
  9.  
  10. struct S {
  11.   int* p = new int(3);
  12.   int x;
  13.  
  14.   S(int el) : x(el) {}
  15.  
  16.   ~S() {
  17.     delete p;
  18.   }
  19. };
  20.  
  21. int main() {
  22. //  void* p = ::operator new(sizeof(std::string));
  23. //  new(p) std::string(3, 'a');
  24. //  new(p) std::string(5, 'b');
  25. //  ::operator delete(p);
  26.  
  27. //  std::string* p = new std::string(4, 'a');
  28. //  std::cout << *p << " ";
  29. //  new(p) std::string(3, 'b');
  30. //  std::cout << *p;
  31. //  delete p;
  32.  
  33.   S* p = new S(4);
  34.  
  35. //  deleter<S> del;
  36. //  del.operator()(p);
  37.  
  38.   std::cout << p->x << " ";
  39.   new(p) S(5);
  40.   std::cout << p->x;
  41.   delete p;
  42. }
  43.  
  44. #include <iostream>
  45.  
  46. template <class Arg>
  47. Arg sum(Arg arg) { return arg; }
  48.  
  49. template <class First, class... Other>
  50. auto sum(First first, Other... other)
  51. {
  52.   return first + sum(other...);
  53. }
  54.  
  55. int main() {
  56.   std::cout << sum(1, 3, 4, 5);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement