You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用不同pmr分配器的容器是否应判定为相等?

C++中分配器感知容器的operator==判定问题

我有一个使用硬编码PMR分配器存储数据的容器,想请教C++社区的共识:仅分配器不同的对象,operator==()应该判定为相等,还是应该遵循强序规则?两种选择都有合理依据。

以下是一个容器的实现示例:

#include <cstdio>
#include <memory_resource>
#include <array>

struct alloc_aware_ctr
{
    using allocator_type = std::pmr::polymorphic_allocator<std::byte>;
    auto get_allocator() {
        return allocator_;
    }

    alloc_aware_ctr(allocator_type allocator = {})
        :   allocator_{allocator}
    {

    }

    auto operator==(const alloc_aware_ctr& other) {
        return allocator_ == other.allocator_;
    }

    allocator_type allocator_;
};

int main()
{

    std::array<std::byte, 10> buffer;

    std::pmr::monotonic_buffer_resource mbr{buffer.data(), buffer.size(), std::pmr::null_memory_resource() };
    std::pmr::polymorphic_allocator<std::byte> pa{&mbr};

    alloc_aware_ctr a;
    alloc_aware_ctr b{pa};

    if (a == b) {
        printf("They are the same!\n");
    }

}

上述实现会区分不同的分配器。


内容的提问来源于stack exchange,提问作者glades

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 12:21:00