c++ - std::vector::shrink_to_fit() 不检查分配器相等性

标签 c++ c++14 stdvector allocator

我有自己的堆栈分配器,它适用于我的测试用例,除了 std::vector::shrink_to_fit()

shr​​ink_to_fit() 尝试使用不同于最初分配内存的有状态分配器来释放内存。

我的理解是,在从一个分配器分配的内存通过另一个分配器释放之前,容器必须检查分配器是否相等。我错了吗?

引自:https://en.cppreference.com/w/cpp/named_req/Allocator

a1 == a2 returns true only if the storage allocated by the allocator a1 can be deallocated through a2. Establishes reflexive, symmetric, and transitive relationship. Does not throw exceptions.

在我的示例中,甚至没有调用相等性检查运算符。

我正在使用 g++ 5.4.0。可以在此处找到完整的代码示例: https://onlinegdb.com/ryG9W5sx4

最佳答案

分配器的拷贝必须比较相等。你的没有。

(因此即使是有状态的分配器也不能有嵌入式存储。)

关于c++ - std::vector::shrink_to_fit() 不检查分配器相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53894851/

相关文章:

c++ - SFINAE 和部分类模板特化

c++ - 如何将boost多精度整数从小端转换为大端?

c++ - std::vector::resize() 与 gcc 4.7.0 的奇怪行为

C++遍历 vector 并删除匹配的字符串

C++ 命名修饰 : QBE_NXZ QAE_NXZ

c++ - 为 XML 元素中的属性调用不同的处理函数

c++ - 每当成员变量可以由可变参数构造时,有条件地启用构造函数

c++ - 如何排序 std::vector 忽略某些数字?

c++ - 在opencv中创建NDWI矩阵

c++ - 无论如何我可以在 visual studio 2005 上使用 visual studio 2015 运行一个已经编码的项目