注意:
[我知道通常的原因是可能的:没有人想到它/草稿纸/ WG21不值得付出努力,我对阻止潜在实现的技术问题比对此功能的实用价值更感兴趣。
我总是觉得很奇怪,它没有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
:两个[ C0]具有不同的分配器?)同时,
vector
适用于异构向量。