c++ - Wrap existing memory with const std::vector? -


ok, learned (a) std::vector uses contiguous memory definition/standard, , (b) &(v[0]) address of contiguous block of memory, can read/write old-skool c-array. like...

void printem(size_t n, int* iary) { (size_t i=0; i<n; ++i) std::cout << iary[i] << std::endl; } void doublem(size_t n, int* iary) { (size_t i=0; i<n; ++i) iary[i] *= 2; }  std::vector<int> v; (size_t i=0; i<100; ++i) v.push_back(i); int* iptr = &(v[0]); doublem(v.size(), iptr); printem(v.size(), iptr); 

ok, that's cool, want go in other direction. have lots , lots of existing code

double computesomething(const std::vector<someclass>& v) { ... } 

if have c-array of objects, can use such code this:

someclass cary[100]; // 100*sizeof(someclass) // populate std::vector<someclass> v; (size_t i=0; i<100; ++i) v.push_back(cary[i]); // v using 100*sizeof(someclass) double x = computesomething(v); 

i (a) without space , (b) without time of inserting redundant copy of data vector. note "just change stupid computesomething, idiot" not sufficient, because there thousands of such functions/methods exhibit pattern not under control and, if many go , change of them.

note because interested in const std::vector& usage, there no worry original memory ever need resized, or modified. want const std::vector constructor, don't know if language allows special constructors const instances of class, like:

namespace std { template <typename t> class vector {   vector() { ... }   vector(size_t n) { ... }   vector(size_t n, const t& t) { ... }   const vector(size_t n, t*) { ... } // can done? ... 

if not possible, how container derived off of std::vector called std::const_vector, (a) construct pointer c-array , size, , (b) purposefully did not implement non-const methods (push_back, resize, etc.), if object typename of const_vector not const object, interface offers const methods makes practically const (and erroneous attempts modify caught @ compile time)?

update: little messing around shows "solves" problem wrt windows-implementation of std::vector:

template <typename t> class vector_tweaker : public std::vector<t> { public:   vector_tweaker(size_t n, t* t) {     _savemyfirst = _myfirst;     _savemylast  = _mylast;     _savemyend   = _myend;     _myfirst = t;     _mylast  = t + n;     _myend   = t + n;   }   ~vector_tweaker() {     _myfirst = _savemyfirst;     _mylast  = _savemylast;     _myend   = _savemyend; // , proceed std::vector destructor   } private:   t* _savemyfirst;   t* _savemylast;   t* _savemyend; }; 

but of course "solution" hideous because (a) offers no protection against base class deleting original memory doing resize() or push_back() (except careful user constructs const vector_tweaker()) -- , (b) specific particular implementation of std::vector, , have reimplemented others -- if indeed other platforms declare std::vector member data protected: microsoft did (seems bad idea).

you can try reference-logic storing introduced in c++11 std::reference_wrapper<>:

someclass cary[100]; // ... std::vector<std::reference_wrapper<someclass>> cv; cv.push_back(cary[i]);   // no object copying done, reference wrapper stored 

or without c11, can create specialization of such template class bytes - char. constructor char* c-array can use ::memcpy: unfortunately use twice memory.

::memcpy(&v[0], c_arr, n); 

something this:

template <typename t> class myvector : public std::vector<t> { };  template <> class myvector<char> : public std::vector<char> {     public:     myvector<char>(char* carr, size_t n) : std::vector<char>(n) {         ::memcpy(&operator[](0), carr, n);     } }; 

what recommend - replace c-arrays vectors possible, no copying needed.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -