簡體   English   中英

Boost 的 small_vector 的迭代器類型不應該滿足 std::contiguous_iterator 概念嗎?

[英]Shouldn't the iterator types of Boost's small_vector satisfy the std::contiguous_iterator concept?

可以通過范圍構造函數和顯式的迭代器對從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做同樣的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的存儲可能就地或在堆上,但始終是連續的。 那么這里有什么問題呢?

std::contiguous_iterator在 C++20 中有 特定要求這些要求不屬於任何 pre-C++20 接口。 由於這些接口在 C++20 之前不存在(尤其是contiguous_iterator_tag ),因此small_vector<T>::iterator無法使用它們。

當然可以添加這樣的接口,條件是 C++20 特性的存在。 但是在 C++20 出現之前編寫的代碼根本無法成為contiguous_iterator而不需要更新它。 雖然存在 C++17“ContiguousIterator”要求,但它沒有標記或其他方法來檢測迭代器是否是連續的。 C++20 在添加了適當的概念時添加了這樣一個標簽(和其他東西)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM