如何声明分配器?
我正在尝试在 cpp 中重新创建内置向量类,以便对类和内存管理进行更多练习。我不断收到一条错误消息,提示“ISO C++ 禁止声明没有类型的‘分配器’”,但我终其一生都不明白为什么。我有什么遗漏的吗?
#include <cstddef>
#include <memory>
template <class T>
class myvector{
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
myvector(){ data = avail = limit = 0; }
explicit myvector(size_type n, const T& t = T()) { create(n,t); }
myvector(const myvector& v){ create(v.begin(), v.end()); }
~myvector() { uncreate(); }
myvector& operator=(const myvector& v)
{
if (&v != this){
uncreate();
create(v.begin(), v.end());
}
return *this;
}
T& operator[](size_type i) { return data[i]; }
const T& operator[](size_type i) const { return data[i]; }
iterator begin(){ return data; }
const_iterator begin() const{ return data; }
iterator end(){ return limit; }
const_iterator end() const{ return limit; }
size_type size(){ return avail - data; }
void push_back(T t)
{
if(avail == limit)
size_type new_size = max(2*(limit-data),ptrdiff_t(1));
iterator new_data = alloc.allocate(new_size);
iterator new_avail = uninitialized_copy(data,avail,new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_size;
alloc.construct(avail++,t);
}
private:
iterator data;
iterator avail;
iterator limit;
allocator<T> alloc;
void create(size_type n, const T& t)
{
data = alloc.allocate(n);
limit = avail = data+n;
uninitialized_fill(data,limit,t);
}
void create(const_iterator i, const_iterator j)
{
data = alloc.allocate(j-i);
limit = avail = uninitialized_copy(i,j,data);
}
void uncreate()
{
if(data){
iterator it = avail;
while(it != data) { alloc.destroy(--it); }
alloc.deallocate(data,limit-data);
}
data = limit = avail = 0;
}
};
I'm trying to recreate the inbuilt vector class in cpp to get a bit more practice with classes and memory management. I keep getting an error that says 'ISO C++ forbids declaration of ‘allocator’ with no type' and I can't figure out why for the life of me. Is there something I'm missing?
#include <cstddef>
#include <memory>
template <class T>
class myvector{
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
myvector(){ data = avail = limit = 0; }
explicit myvector(size_type n, const T& t = T()) { create(n,t); }
myvector(const myvector& v){ create(v.begin(), v.end()); }
~myvector() { uncreate(); }
myvector& operator=(const myvector& v)
{
if (&v != this){
uncreate();
create(v.begin(), v.end());
}
return *this;
}
T& operator[](size_type i) { return data[i]; }
const T& operator[](size_type i) const { return data[i]; }
iterator begin(){ return data; }
const_iterator begin() const{ return data; }
iterator end(){ return limit; }
const_iterator end() const{ return limit; }
size_type size(){ return avail - data; }
void push_back(T t)
{
if(avail == limit)
size_type new_size = max(2*(limit-data),ptrdiff_t(1));
iterator new_data = alloc.allocate(new_size);
iterator new_avail = uninitialized_copy(data,avail,new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_size;
alloc.construct(avail++,t);
}
private:
iterator data;
iterator avail;
iterator limit;
allocator<T> alloc;
void create(size_type n, const T& t)
{
data = alloc.allocate(n);
limit = avail = data+n;
uninitialized_fill(data,limit,t);
}
void create(const_iterator i, const_iterator j)
{
data = alloc.allocate(j-i);
limit = avail = uninitialized_copy(i,j,data);
}
void uncreate()
{
if(data){
iterator it = avail;
while(it != data) { alloc.destroy(--it); }
alloc.deallocate(data,limit-data);
}
data = limit = avail = 0;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是 std::allocatoralloc;,标准库中的所有内容都包含在命名空间
std
中。It should be,
std::allocator<T> alloc;
, everything in the standard library is contained within the namespacestd
.另请参阅 http://code .google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h。它包括 GCC 错误的解决方法。
注意:标准 C++ 容器只需检查 vector::resize 上的溢出(希望现在已经改变)。 ESAPI 分配器在所有分配期间进行检查。
Alos see http://code.google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h. It includes work arounds for GCC bugs.
Note: the standard C++ containers only have to check for overflow on vector::resize (hopefully that has changed by now). The ESAPI allocator checks during all allocations.