I have an advanced C++ question: Suppose I have a mmap_allocator template class, which is a subclass of std::allocator template class and a mmappable_vector template class which is a subclass of std::vector template class:
template <typename T> class mmap_allocator: public std::allocator<T> { ... }; template <typename T, typename A = mmap_allocator<T> > class mmappable_vector: public std::vector<T, A> { ... }; What I can do is convert from a mmappable_vector (with an mmap_allocator) to a std::vector (with the standard allocator) using a function template:
template <typename T> std::vector<T> to_std_vector(const mmappable_vector<T> &v) { return std::vector<T>(v.begin(), v.end()); } but the other way seems not to be possible:
template <typename T> mmappable_vector<T> to_mmappable_vector(const std::vector<T> &v) { return mmappable_vector<T>(v.begin(), v.end()); } The problem when defining a constructor like:
typedef typename std::vector<T, A>::iterator iterator; mmappable_vector(iterator from, iterator to): std::vector<T,A>(from, to) { } this uses iterators with the mmap_allocator and hence does not match the call in to_mmappable_vector. On the other hand defining a constructor:
mmappable_vector(std::vector<T,std::allocator<T> > v): std::vector<T,std::allocator<T> >(v) { } fails because the
std::vector<T,std::allocator<T> > is not a base class of the mmappable vector.
How do I write a function template that converts std::vectors to mmappable_vectors? Is this possible at all within C++?
Thanks for any insights,
- Johannes