When I try to compile the code from this post (see below) in Visual Studio 2019, I receive the following compiler errors at line of the templated copy ctor:
Error C2439 'PreAllocator<_Newfirst>::memory_ptr': member could not be initialized
Error C2440 'initializing': cannot convert from 'T *const ' to 'T *'
Error C2248 'PreAllocator<int>::memory_size': cannot access private member declared in class 'PreAllocator<int>'
Error C2248 'PreAllocator<int>::memory_ptr': cannot access private member declared in class 'PreAllocator<int>'
Is this a compiler bug or am I missing something?
template <typename T>
class PreAllocator
{
private:
T* memory_ptr;
std::size_t memory_size;
public:
typedef std::size_t size_type;
typedef T* pointer;
typedef T value_type;
PreAllocator(T* memory_ptr, std::size_t memory_size) : memory_ptr(memory_ptr), memory_size(memory_size) {}
PreAllocator(const PreAllocator& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};
template<typename U>
PreAllocator(const PreAllocator<U>& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};
template<typename U>
PreAllocator& operator = (const PreAllocator<U>& other) { return *this; }
PreAllocator<T>& operator = (const PreAllocator& other) { return *this; }
~PreAllocator() {}
pointer allocate(size_type n, const void* hint = 0) { return memory_ptr; }
void deallocate(T* ptr, size_type n) {}
size_type max_size() const { return memory_size; }
};
int main()
{
int my_arr[100] = { 0 };
std::vector<int, PreAllocator<int>> my_vec(0, PreAllocator<int>(&my_arr[0], 100));
}