C++17 introduced the concept of ContiguousIterator http://en.cppreference.com/w/cpp/iterator.
However it doesn't seem that there are plans to have a contiguous_iterator_tag
(in the same way we now have random_access_iterator_tag
) reported by std::iterator_traits<It>::iterator_category
.
Why is contiguous_iterator_tag
missing?
Is there a conventional protocol to determine if an iterator is Contiguous? Or a compile time test?
In the past I mentioned that for containers if there is a .data()
member that converts to a pointer to ::value
type and there is .size()
member convertible to pointer differences, then one should assume that the container is contiguous, but I can't pull an analogous feature of iterators.
One solution could be to have also a data
function for contiguous iterators.
Of course the Contiguous concept works if &(it[n]) == (&(*it)) + n
, for all n
, but this can't be checked at compile time.
EDIT: I found this video which puts this in the more broader context of C++ concepts. CppCon 2016: "Building and Extending the Iterator Hierarchy in a Modern, Multicore World" by Patrick Niedzielski. The solution uses concepts (Lite) but at the end the idea is that contiguous iterators should implement a pointer_from
function (same as my data(...)
function).
The conclusion is that concepts will help formalizing the theory, but they are not magic, in the sense that someone, somewhere will define new especially named functions over iterators that are contiguous.
The talk generalizes to segmented iterators (with corresponding functions segment
and local
), unfortunatelly it doesn't say anything about strided pointers.
EDIT 2020:
The standard now has
struct contiguous_iterator_tag: public random_access_iterator_tag { };
contiguous_iterator_tag
missing? ← because it will silently break pre-C++17 code that assumedstd::vector::iterator
is exactly a random access iterator? – Narcorandom_access_iterator
derived fromcontiguous_iterator_tag
). It would be unfortunate if the break would happen just because the trait was used incorrectly. – Chuipek.data()
member or adata(ContiguousIterator it)
function for all contiguous iterators, this is in analogy to containers which currently have the.data()
member when they are contiguous. – Chuipek