gpt4 book ai didi

c++ - 在 C++20 中 std::vector 运算符 == 对具有不同分配器的 vector 不起作用有什么原因吗?

转载 作者:行者123 更新时间:2023-12-01 14:03:46 25 4
gpt4 key购买 nike

笔记:

我知道通常的原因是可能的:没有人想到它/写过论文/WG21 认为这不值得付出努力,我对阻止潜在实现的技术问题更感兴趣,而不是对这个功能的效用值(value)感兴趣。

我总是觉得奇怪,这不是 work (甚至在概念之前,因为我们可以使用 enable_if)

#include <vector>
#include <boost/align/aligned_allocator.hpp>
int main()
{
std::vector<int> a{1,2,3};
std::vector<int, boost::alignment::aligned_allocator<int,64>> b{1,2,3};
return a==b;
}

原因是分配器不会影响存储在容器中的值(我知道值可能会在运算符 == 中使用它们的地址,我说的是“正常”类型)。

所以我的问题是:
如果我们想用 C++20 的概念来做这件事,我们可以在不破坏任何现有代码的情况下引入这个功能吗?

最佳答案

没有任何技术问题。实现当然很简单:

template <std::equality_comparable T, typename A1, typename A2>
bool operator==(std::vector<T, A1> const& lhs, std::vector<T, A2> const& rhs) {
return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}

那么为什么要止步于分配器呢?为什么我不能将 vector<int>vector<long> 进行比较?

P0805 是扩大比较集以允许比较混合类型和混合分配器容器的建议。它被批准用于 C++20,但没有通过,仍然需要一些工作来适应新的 C++20 概念(特别是 equality_comparable_with 需要 common_reference :两个 vector 之间具有不同分配器的共同引用是什么?)

同时, std::ranges::equal(v1, v2) 适用于异构 vector 。

关于c++ - 在 C++20 中 std::vector 运算符 == 对具有不同分配器的 vector 不起作用有什么原因吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60836606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com