Reputation: 61
Trying to figure out how to use std :: allocator.
#include <iostream>
template <typename T, typename A>
struct vector_base
{
A allocator;
T* data;
size_t size;
size_t space;
vector_base(const A &alloc, size_t n)
:allocator(alloc), data(alloc.allocate(n)), size(n), space(n)
{}
~vector_base() {allocator.deallocate(data, space);}
};
int main() {
std::allocator<int> my_allocator;
vector_base<int, std::allocator<int>> vector(my_allocator, 10);
return 0;
}
Error:
error: passing ‘const std::allocator’ as ‘this’ argument discards qualifiers [-fpermissive] :allocator(alloc), data(alloc.allocate(n)), size(n), space(n)
Upvotes: 1
Views: 401
Reputation: 36463
alloc
is a const&
, you're trying to call allocate
on it which is a non-const method, that's not allowed.
Call allocate
on your initialized member instead:
vector_base(const A &alloc, size_t n)
:allocator(alloc), data(allocator.allocate(n)), size(n), space(n)
{}
Upvotes: 4