CUJ:标准库:Allocator能做什么?[11]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 taodm 的 blog

        first = allocator::allocate(n);

        size_type i;

        try {

          for (i = 0; i < n; ++i) {

            allocator::construct(first + i, x);

          }

        }

        catch(...) {

          for(size_type j = 0; j < i; ++j) {

            allocator::destroy(first + j);

          }

          allocator::deallocate(first, n);

          throw;

        }

      }

    }

    (你可能会问为什么我们手写了这个循环;std::uninitialized_fill()不是已经做我们所需要的东西?几乎,但不完全相同。我们必须调用allocator的构造成员函数而不是简单的pacement new。也许未来的c++标准将会包含一个接受allocator为参数的uninitialized_fill()函数,从而使得不再需要这个显式循环。

    析构函数比较简单,因为我们不需要担心异常安全:t的析构函数被假设为从不抛出异常。

    template <class t, class allocator>

本文关键:CUJ、allocator、STL、Matt Austern
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top