c++ - Is there any library that provides containers for non-copyable types that are not default-contructible? -
i looking library provides containers std::array
(compile-time fixed size, without support aggregate initialization) , std::vector
(variable size, continuous memory) work types non-copyable , not default-constructible.
specifically, want able pass set of functions constructor used obtain arguments constructors of contained objects. emplace_back
, constructor , using lazily evaluated arguments.
here (naturally not working) example:
class stubborn_type : boost::noncopyable { public: explicit stubborn_type(int value) : value(value) {} private: const int value; }; struct generate_values { generate_values(int initial_value = 0) : current_value(initial_value) {} int operator()() { return current_value++; } private: int current_value; }; /* should create vector containing 10 elements initialized values [0..9] in order. */ magic::vector<stubborn_type> data(10, generate_values());
i need solution compatible c++03 (since means no variadic templates prefer boost approach of using preprocessor magic generate overloads different numbers of arguments, reasonable fixed limit fine well). exist? if not, there libraries achieve goal (for instance, boost.in place factory useful, not support lazy arguments).
i not know ready solution, however, not hard it. outline of array may this
#include <cstddef> typedef char saum; // smallest addressable unit of memory namespace magic { template<class t> class iarray { t *const _ptr; const size_t _length; public: operator t* () { return _ptr; } size_t length() const { return _length; } private: iarray<t>(t* ptr, size_t length) : _ptr(ptr), _length(length) {} template<class s, size_t length> friend class array; }; template<class t> class generator { public: virtual void operator() (t* place) = 0; }; template<class t, size_t length> class array { saum buffer[sizeof(t) * length]; public: array(generator<t>& generate) { (size_t = 0; < length; i++) generate((t*)buffer + i); } ~array() { (size_t = 0; < length; i++) ((t*)buffer)[i].~t(); } operator iarray<t> () { return iarray<t>((t*)buffer, length); } operator t* () { return (t*)buffer; } }; }
i'm little tired, forgive me not thinking out better names. nevertheless, shall job. maybe, generator solution not beautiful, still gives flexibility. believe more feathers won't case problem.
an example
#include <new> #include <iostream> class stubborn { public: stubborn(int value) : value(value*2) { } const int value; private: stubborn(const stubborn&); stubborn& operator=(const stubborn&); }; struct stubborngen : public magic::generator<stubborn> { stubborngen() : current_value(0) {} void operator() (stubborn* place) { new(place) stubborn(current_value++); } private: int current_value; }; void f(magic::iarray<stubborn> stubs) { std::cout << stubs[0].value << stubs[1].value << stubs[2].value << stubs[3].value << " " << stubs.length() << std::endl; } int main(int argc, char *argv[]) { stubborngen gen; magic::array<stubborn, 4> stubs(gen); f(stubs); }
edit: corrected, dyp, plus generator tweak.
it important define copy constructor , assignment operator array, avoid problem pointed dyp. -- how? - depends on want. making them private 1 way. , doing non-shallow copy another. - in second case, mechanism of template instantiation should prevent errors, when applying non-copiable class array.
Comments
Post a Comment