否,没有编译时特性。
draft C++1z Standard 将连续性定义为迭代器范围的运行时属性。注意没有编译时间std::contiguous_iterator_tag对应这个迭代器类别。
24.2 迭代器要求 [iterator.requirements]
24.2.1 一般 [iterator.requirements.general]
5 个迭代器,进一步满足以下要求,对于积分
值n 和可取消引用的迭代器值a 和(a + n), *(a + n)
相当于*(addressof(*a) + n),称为连续
迭代器。 [注:例如,类型“pointer to int”是一个
连续迭代器,但reverse_iterator<int *> 不是。对于一个有效的
迭代器范围[a,b) 和可取消引用的a,对应的范围
用指针表示的是[addressof(*a),addressof(*a) + (b - a));b
可能无法取消引用。 ——尾注]
在运行时对此进行测试的一种方法是
#include <array>
#include <deque>
#include <list>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>
template<class I>
auto is_contiguous(I first, I last)
{
auto test = true;
auto const n = std::distance(first, last);
for (auto i = 0; i < n && test; ++i) {
test &= *(std::next(first, i)) == *(std::next(std::addressof(*first), i));
}
return test;
}
int main()
{
auto l = std::list<int> { 1, 2, 3 };
auto m = std::map<int, int> { {1, 1}, {2,2}, {3,3} };
auto u = std::unordered_multiset<int> { 1, 1, 1 };
auto d = std::deque<int>(4000);
int c[] = { 1, 2, 3 };
auto a = std::array<int, 3> {{ 1, 2, 3 }};
auto s = std::string {"Hello world!"};
auto v = std::vector<int> { 1, 2, 3, };
std::cout << std::boolalpha << is_contiguous(l.begin(), l.end()) << "\n";
std::cout << std::boolalpha << is_contiguous(m.begin(), m.end()) << "\n";
std::cout << std::boolalpha << is_contiguous(u.begin(), u.end()) << "\n";
std::cout << std::boolalpha << is_contiguous(d.begin(), d.end()) << "\n";
std::cout << std::boolalpha << is_contiguous(d.begin(), d.begin() + 1000) << "\n";
std::cout << std::boolalpha << is_contiguous(std::begin(c), std::end(c)) << "\n";
std::cout << std::boolalpha << is_contiguous(a.begin(), a.end()) << "\n";
std::cout << std::boolalpha << is_contiguous(s.begin(), s.end()) << "\n";
std::cout << std::boolalpha << is_contiguous(v.begin(), v.end()) << "\n";
std::cout << std::boolalpha << is_contiguous(v.rbegin(), v.rend()) << "\n";
}
Live Example。这将打印false 用于list、map 和unordered_multimap,以及true 用于C 阵列,以及std::array、string 和vector。它为deque 内的小子范围打印true,为大子范围打印false。它还为由反向迭代器组成的迭代器范围打印false。
更新:由@T.C. 评论。最初的N3884 提案确实有一个
struct contiguous_iterator_tag : random_access_iterator_tag {};
以便迭代器类别上的标签调度不会中断。但是,这会破坏具有random_access_iterator_tag 上的类模板特化的非惯用代码。因此,当前草案不包含新的迭代器类别标签。