How to fill an array of unique_ptr?

江枫思渺然 提交于 2019-12-09 12:57:47

问题


Is it possible to use std:fill to fill an array of unique_ptrs? The intention is to have distinct pointers to distinct objects which are initialized with the same parameters.

For example:

std::unique_ptr<int> ar[3];
std::fill(ar.begin(), ar.end(), make_unique_for_each_element_somehow<int>(1));

回答1:


No, but this is what std::generate is for.

Instead of being given a single value that's copied throughout the target range, std::generate is given a "generator" function that creates each value as needed.

So, probably, something like this:

std::unique_ptr<int> ar[3];
std::generate(
   std::begin(ar),
   std::end(ar),
   []() { return std::make_unique<int>(1); }
);

I haven't tried it, and don't know whether you need to fiddle with it at all in order to avoid problems stemming from non-copyability. Hopefully move semantics are enough.

(Of course, in C++11, you will need your own make_unique function.)

By the way, your .begin() and .end() were wrong (arrays don't have member functions), so (thanks to a reminder from songyuanyao) I have corrected those.




回答2:


I don't think you can do it with std::fill() but it is trivial to do with std::generate():

std::unique_ptr<int> array[10];
std::generate(std::begin(array), std::end(array),
              []{ return std::unique_ptr<int>(new int(17)); });



回答3:


Another solution:

std::unique_ptr<int> ar[3];

size_t i;
for (i = 0; i < elementsof(ar); ++i) {
    ar[i].reset(new int);
}


来源:https://stackoverflow.com/questions/41365505/how-to-fill-an-array-of-unique-ptr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!