Advertisement
Ilya_konstantinov

main.cpp

Oct 10th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. // #include "shared.h"
  2. #include <iostream>
  3. #include "shared.h" // for size_t
  4. // #include "tmp.h" // for size_t
  5. #include <iostream> // for std::nullptr_t
  6. #include <cassert>
  7.  
  8. #define REQUIRE assert
  9.  
  10. template<typename T>
  11. class A {
  12. private:
  13.     T* ptr_;
  14. public:
  15.     template<typename Y>
  16.     requires (std::derived_from<T, Y> || std::same_as<T, Y>)
  17.     explicit A(Y* ptr) : ptr_(static_cast<T*>(ptr)) {
  18.         // some code
  19.     }
  20. };
  21.  
  22. struct Pinned {
  23.     Pinned(int tag) : tag_(tag) {
  24.     }
  25.  
  26.     Pinned(const Pinned& a) = delete;
  27.     Pinned(Pinned&& a) = delete;
  28.  
  29.     Pinned& operator=(const Pinned& a) = delete;
  30.     Pinned& operator=(Pinned&& a) = delete;
  31.  
  32.     ~Pinned() = default;
  33.  
  34.     int GetTag() const {
  35.         return tag_;
  36.     }
  37.  
  38. private:
  39.     int tag_;
  40. };
  41.  
  42. int main() {
  43.     /**/
  44.     SharedPtr<std::string> a(new std::string("aba"));
  45.     std::string* ptr;
  46.     {
  47.         SharedPtr b(a);
  48.         SharedPtr c(a);
  49.         ptr = c.Get();
  50.     }
  51.     REQUIRE(ptr == a.Get());
  52.     REQUIRE(*ptr == "aba");
  53.  
  54.     SharedPtr<std::string> b(new std::string("caba"));
  55.     {
  56.         SharedPtr c(b);
  57.         SharedPtr d(b);
  58.         d = std::move(a);
  59.         REQUIRE(*c == "caba");
  60.         REQUIRE(*d == "aba");
  61.         b.Reset(new std::string("test"));
  62.         REQUIRE(*c == "caba");
  63.     }
  64.     REQUIRE(*b == "test");
  65.  
  66.     SharedPtr<std::string> end;
  67.     {
  68.         SharedPtr<std::string> d(new std::string("delete"));
  69.         d = b;
  70.         SharedPtr c(std::move(b));
  71.         REQUIRE(*d == "test");
  72.         REQUIRE(*c == "test");
  73.         d = d;
  74.         c = end;
  75.         d.Reset(new std::string("delete"));
  76.         end = d;
  77.     }
  78.  
  79.     REQUIRE(*end == "delete");
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement