我有自己的堆栈分配器,它适用于我的测试用例,除了 std::vector::shrink_to_fit()
shrink_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/