简体   繁体   中英

Set class member unique_ptr<T[]> array without copying

I have a class containing a c-style array managed with unique_ptr. I want to provide a constructor:

class A {
  unique_ptr<T[]> p;
public:
  A(int d, X x) : p(new T[d]) {
    //Transfer from x to p without copying
  }
}

so that I can build my object with something like:

int main(..) {
  A a{n,{expr1,expr2,..}};
}

where {expr1,expr2,..} contains the values (evaluated at runtime) for the inizialization. Since this list is temporary, it seems to me a waste of resources to build it, copying its values into the actual object and discard it.

I believe that with move semantincs, rvalues and all the nice features of C++11 there should be a solution for this simple task, but I could not find it (I'm quite new in C++).

I would like to stick with c-style arrays and don't move to std::vectors. Is there a solution?

There are tow points I'd like to make here.

  1. AFAICS, std::unique_ptr<T[]> offers you very little benefit over the standard C++ solution of using a std::vector<T> , namely a reduced memory footprint (64 instead of 128 bytes for the container itself on a 64bit machine and potentially also of the amount of heap used), but see the discussion here . Any newby to C++ should stick to std::vector .

  2. Move semantics are only beneficial for objects that manage memory on the heap ('free store'). So only if your expr1 , expr2 etc are object which themselves keep track of allocated memory, does moving have any meaning. This does not appear the case here, so just copy.

Yes, you can use perfect forwarding:

#include <memory>
#include <string>

struct S
{
    S(int) {}
    S(S const&) = delete;
    S(S&&) = default;
};

template<typename T>
struct A
{
    std::unique_ptr<T[]> p;

    template<typename... Args>
    A(int d, Args&&... args)
        : p(new T[sizeof...(args)]{std::forward<Args>(args)...})
    {
    }
};

int main()
{
    A<int> a(0, 1, 2, 3, 4);

    A<std::string> b(0, "hello", "world!", "\n");

    S s(0);
    A<S> c(0, std::move(s), 2, 3);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM