NoS*_*tAl 4 c++ allocator c++-concepts c++20
笔记:
我知道通常的原因是可能的:没有人想到它/写过论文/WG21 认为这不值得付出努力,我对阻止潜在实施的技术问题更感兴趣,而不是对这个功能的效用价值感兴趣。
我一直觉得很奇怪,这并不工作(甚至概念之前,因为我们可以使用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;
}
Run Code Online (Sandbox Code Playgroud)
原因是分配器不会影响存储在容器中的值(我知道值可能会在运算符 == 中使用它们的地址,我说的是“正常”类型)。
所以我的问题是:如果我们想用 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());
}
Run Code Online (Sandbox Code Playgroud)
那么为什么要止步于分配器呢?为什么我不能将 avector<int>与 a进行比较vector<long>?
P0805是扩大比较集以允许比较混合类型和混合分配器容器的建议。它被批准用于 C++20,但没有通过,仍然需要一些工作来适应新的 C++20 概念(特别是,equality_comparable_with需要common_reference:vector具有不同分配器的两个s之间的公共引用是什么?)
同时,std::ranges::equal(v1, v2)适用于异构向量。