【问题标题】:Shouldn't the iterator types of Boost's small_vector satisfy the std::contiguous_iterator concept?Boost 的 small_vector 的迭代器类型不应该满足 std::contiguous_iterator 概念吗?
【发布时间】:2021-07-15 11:06:52
【问题描述】:

可以通过范围构造函数和一对显式迭代器从std::vector(作为连续容器的原型)构造std::span

#include <span>
#include <vector>

std::vector<int> owning;
std::span<int> view1{owning.begin(), owning.end()}; // works
std::span<int> view2{owning}; // works

但是,当使用 Boost 容器库中的 small_vector(也应该是连续的)执行相同操作时,我遇到了问题 (godbolt):

#include <span>
#include <boost/container/small_vector.hpp>

boost::container::small_vector<int, 10> owning;
std::span<int> view1{owning.begin(), owning.end()}; // Error
std::span<int> view2{owning}; // Error (works with clang + libc++ though)

问题似乎是boost::small_vector 的迭代器不满足std::contiguous_iterator 概念,即gccclanglibc++libstdcxxgodbolt)都失败了:

static_assert(
    std::contiguous_iterator<boost::container::small_vector<int, 10>::iterator>);

small_vector 的存储可能在原地或堆上,但始终是连续的。那么这里有什么问题呢?

【问题讨论】:

  • 快速搜索显示这刚刚在 github repository 中修复
  • 太棒了!我在 repo 上进行了搜索,但只有 PR,而不是问题……我的错,谢谢!如果您将您的评论变成答案,我很乐意接受。

标签: c++ boost c++20 boost-container


【解决方案1】:

std::contiguous_iterator 在 C++20 中有 specific requirements,它们不属于任何 C++20 之前的接口。由于这些接口在 C++20 之前不存在(尤其是 contiguous_iterator_tag),因此 small_vector&lt;T&gt;::iterator 无法使用它们。

当然可以添加这样一个接口,条件是存在 C++20 特性。但是在 C++20 出现之前编写的代码根本无法成为contiguous_iterator,而无需对其进行更新。虽然有 C++17“ContiguousIterator”要求,但它没有标签或其他方式来检测迭代器是否是连续的。 C++20 在添加适当的概念时添加了这样的标签(和其他东西)。

【讨论】:

    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    相关资源
    最近更新 更多