#include <cstddef>
#include <cstdlib>
#include <limits>
#include <utility>
#include <vector>
template <typename T>
class mallocator // because malloc and allocator :O
{
public:
typedef T value_type;
typedef value_type * pointer;
typedef value_type const * const_pointer;
typedef value_type & reference;
typedef value_type const & const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
template <typename U>
struct rebind
{
typedef mallocator<U> other;
};
inline explicit mallocator() {}
inline ~mallocator() {}
inline explicit mallocator(mallocator const &) {}
template <typename U>
inline explicit mallocator(mallocator<U> const &) {}
inline pointer address(reference r) { return std::addressof(r); }
inline const_pointer address(const_reference r) { return std::addressof(r); }
inline pointer allocate(size_type cnt, void const * = nullptr)
{
return std::malloc(cnt * sizeof(value_type));
}
inline void deallocate(pointer p, size_type)
{
std::free(p);
}
inline size_type max_size() const
{
return std::numeric_limits<size_type>::max() / sizeof(T);
}
inline void construct(pointer p, const_reference t) { new(p) T(t); }
inline void destroy(pointer p) { p->~T(); }
inline bool operator == (mallocator const &) { return true; }
inline bool operator != (mallocator const &) { return false; }
};
template <typename Type, typename Allocator>
class raw_vector
: public std::vector<Type, Allocator>
{
using std::vector<Type, Allocator>::_Vector_base::_M_impl;
public:
template <typename Iterator>
raw_vector(Iterator start, Iterator end, Allocator const & a = Allocator())
: std::vector<Type, Allocator>(a)
{
// implementation defined
_M_impl._M_start = start;
_M_impl._M_finish = _M_impl._M_end_of_storage = end;
}
};
int main()
{
char * foo = static_cast<char *>(std::malloc(12));
// no deep copy here:
raw_vector<char, mallocator<char>> raw(foo, foo + 12);
// still no deep copy
std::vector<char, mallocator<char>> normal(std::move(raw));
// mallocator is important, so std::free is used instead of delete[]
return 0;
}