我是否有必要为使用任意分配器分配的原始类型数组调用 allocator.construct(),如下面的代码清单所示?该类不需要将分配的内存初始化为任何特定值,因此在我看来,使用新分配的内存块调用 allocator.construct() 是不必要的。考虑到数组总是由原始类型组成,不调用这个方法有什么危险吗?
template <class T, template <class> class Allocator = std::allocator>
class foo
{
public:
typedef Allocator<T> allocator;
typedef typename allocator::pointer pointer;
private:
unsigned size_;
allocator alloc_;
pointer t_;
public:
foo(unsigned n) throw(std::bad_alloc) : size_(n), alloc_(),
t_(alloc_.allocate(n))
{
// Note that I do not call alloc_.construct() here.
}
~foo() { alloc_.deallocate(t_, size_); }
};