mardi 1 août 2023

Is there a way to concatenate 2 shared pointers without copying them?

I am basically wondering if there is a way to concatenate 2 shared_ptr's into one without copying memory? The way I currently would concatenate them is:

int main(){
  std::shared_ptr<float[]> _a = std::make_unique<float[]>(10);
  std::shared_ptr<float[]> _b = std::make_unique<float[]>(20);
  std::shared_ptr<float[]> concat = std::make_unique<float[]>(30);
  std::copy(_a.get(), _a.get() + 10, concat.get());
  std::copy(_b.get(), _b.get() + 20, concat.get() + 10);
  return 0;
}

Is there a way to just make a shared_ptr point to both on a certain range? I realize I could make a wrapper using the following:

struct concat{
  std::shared_ptr<float[]> a;
  std::shared_ptr<float[]> b;
  size_t sa, sb;
  concat(std::shared_ptr<float[]> _a, std::shared_ptr<float[]> _b, size_t _sa, size_t _sb)
  :a(_a), b(_b), sa(_sa), sb(_sb)
{}
  float& operator[](const size_t& index){
  return (index > sa ? b[index - sa] : a[index]);
  }

};

Is there a more built-in way to do this with shared pointers?

Aucun commentaire:

Enregistrer un commentaire