lundi 23 février 2015

How best to make iterators for a plain array within a unique_ptr in C++11?

I want to use the richness of with arrays held by unique_ptr. Here's the code I'd like to write ajuxt the code I am currently obliged to write:



void question() {
const int len = 10;
int a[len];
unique_ptr<int[]> p(new int[len]);

// i can do this with a bare array
for_each(begin(a), end(a), [](int& v) { v = 0; });

// but this doesn't compile cuz unique_ptr<T[]> doesn't implement dereference
// for_each(begin(*p), end(*p), [](int& v) { v = 0; });

// this works, but ugly, and begin() and end() are not abstracted.
for_each(&p[0], &p[len], [](int& v) { v = 0; });

// how best to make iterators for an array within a unique_ptr?
}


Or might I be much better off using a container class instead of an array?


Aucun commentaire:

Enregistrer un commentaire