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

Viewed 576

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?

2 Answers
Related