To learn about the intricacies of C++11 I am playing aroung with unique_ptr
a bit.
I wonder, is there any way to use iota
to initialize an Container of unique_ptr
?
I started with the unique-ptr-less solution which works fine:
std::vector<int> nums(98); // 98 x 0
std::iota(begin(nums), end(alleZahlen), 3); // 3..100
Now lets do it as far as we can using unique_ptr
std::vector<std::unique_ptr<int>> nums(98); // 98 x nullptr
std::unique_ptr three{ new int{3} };
std::iota(begin(nums), end(nums), std::move{three});
This fails obviously. Reasons:
- Although I marked
three
withmove
as a&&
this may not be sufficient to copy/move the initial value into the container. ++initValue
will also not work, becauseinitValue
is of typeunique_ptr<int>
, and there is nooperator++
defined. But: we could define a free functionunique_ptr<int> operator++(const unique_ptr<int>&);
and that would take care of that at least.- But to copy/move the results from that operation is again not permitted in
unique_ptr
and this time I can not see how I could trick the compiler into usingmove
.
Well, that's where I stopped. And I wonder if I miss some interesting idea on how to tell the compiler that he may move
the results of the operator++
. Or are there other hindrances, too?