Facebook
From Speedy Agouti, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 87
  1.         #include <memory>
  2.     #include <inttypes.h>
  3.     #include <vector>
  4.    
  5.     struct OtherClass
  6.         {
  7.                 OtherClass() = default;
  8.                 ~OtherClass() = default;
  9.  
  10.                 OtherClass(const OtherClass&) = delete;
  11.                 OtherClass& operator=(const OtherClass&) = delete;
  12.  
  13.                 OtherClass(OtherClass&&) = default;
  14.                 OtherClass& operator=(OtherClass&&) = default;
  15.         };
  16.  
  17.         struct Buf
  18.         {
  19.                 static std::shared_ptr<Buf> Create(size_t count, const std::shared_ptr<OtherClass>*& others)
  20.                 {
  21.                         return std::make_shared<Buf>(count, others);
  22.                 }
  23.  
  24.                 static std::shared_ptr<Buf> Create(size_t count, std::shared_ptr<OtherClass>*&& others)
  25.                 {
  26.                         return std::make_shared<Buf>(count, std::move(others));
  27.                 }
  28.  
  29.                 Buf(size_t count, const std::shared_ptr<OtherClass>*& other)
  30.                 {
  31.                         others.reserve(count);
  32.                         for(size_t i = 0; i < count; ++i) {
  33.                                 others.push_back(other[i]);
  34.                         }
  35.                 }
  36.  
  37.                 Buf(size_t count, std::shared_ptr<OtherClass>*&& other)
  38.                 {
  39.                         others.reserve(count);
  40.                         for(size_t i = 0; i < count; ++i) {
  41.                                 others.push_back(std::move(other[i]));
  42.                         }
  43.                 }
  44.  
  45.                 Buf() = default;
  46.                 ~Buf() = default;
  47.  
  48.                 Buf(const Buf&) = delete;
  49.                 Buf& operator=(const Buf&) = delete;
  50.  
  51.                 Buf(Buf&&) = default;
  52.                 Buf& operator=(Buf&&) = default;
  53.  
  54.                 std::vector<std::shared_ptr<OtherClass>> others;
  55.         };
  56.    
  57.     std::shared_ptr<Buf> buf;
  58.    
  59.     int main()
  60.     {
  61.                 std::shared_ptr<OtherClass> other;
  62.  
  63.                         //this calls Buf(size_t count, std::shared_ptr<OtherClass>*&& other)
  64.                         //instead of
  65.                         //Buf(size_t count, const std::shared_ptr<OtherClass>*& other)
  66.                         //why?
  67.                         buf = Buf::Create(1, &other);
  68.  
  69.                         //and this is ok, Buf(size_t count, std::shared_ptr<OtherClass>*&& other), because of std::move()
  70.                         std::shared_ptr<OtherClass> other;
  71.                         buf = Buf::Create(1, std::move(&other));
  72.     }