在 C++ 11 分配器规则中略有变化,但我认为它不会影响你的问题。
您可能想首先了解标准对此有何规定。但您实际上想要检查您的特定 STL 实现是否符合标准并且不包含错误。
对于第二部分,我强烈建议您查看来源并进行检查,实际上并不那么难。
另外,您可以编写测试来查看它是否确实正常工作:
- Create custom allocator:
- 使用一些自定义类型作为指针,const指针;
- In
construct()
, destruct()
计算呼叫次数;
- Create
YourCustomType
与分配器一起使用,分配器还计算构造/销毁的数量。
- Now, create
std::vetor<YourCustomType, YourCustomAllocator<YourCustomType>>
instance, insert some elements, clear the vector, destroy it and see if:
- 数量
construct()
destruct()
调用等于构造破坏的数量YourCustomType
.
typeid(YourCustomAllocator::pointer) == typeid(std::vetor<YourCustomType, YourCustomAllocator<YourCustomType>>::pointer)
这样您就可以确保所有限制均适用。
至于问题的第一部分,这是一个旧的 C++ 标准 http://www.csci.csusb.edu/dick/c++std/cd2/lib-containers.html(不是 C++ 11)。
1(正确实现的)向量不可能凭空获取分配器。它将使用您提供的任何分配器,并将其用于所有事情。至于operator==,它是在boost的分配器中实现的,因此让operator==按要求工作是boost的问题。虽然我无法在其中找到确认信息standard http://www.csci.csusb.edu/dick/c++std/cd2/lib-containers.html.
2除非有bugstd::vector<T, YourAllocator>::pointer
应该是分配器的指针。cppreference.com 说 http://en.cppreference.com/w/cpp/container/vector,以及标准说 http://www.csci.csusb.edu/dick/c++std/cd2/lib-containers.html,(查找“模板类向量”):
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
虽然相同的标准是这样说的分配器 http://www.csci.csusb.edu/dick/c++std/cd2/lib-utilities.html#lib.allocator.requirements:
本国际标准中描述的容器的实现
允许假设他们的分配器模板参数满足
除表 6 中的要求外,还需满足以下两项附加要求。
--给定分配器类型的所有实例都需要相互连接
变化无常,并且总是相互比较。
--typedef 成员指针、const_pointer、size_type 和 diff-
ence_type 必须为 T*、T const*、size_t 和 ptrdiff_t,
分别。
因此,实际上标准不允许使用任何指针类型,但我的猜测是实际的 STL 实现会起作用。
3只需检查std::vector<T>::clear()
方法实现以查看是否调用了 allocator::destroy。查看std::vector<T>::resize()
方法的实现以查看是否使用了 allocator::construct。我无法找到调用销毁和构造的要求标准 http://www.csci.csusb.edu/dick/c++std/cd2/lib-containers.html.