连续迭代器检测 [英] Contiguous iterator detection

查看:124
本文介绍了连续迭代器检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 17引入了ContiguousIterator的概念 http://en.cppreference.com/w / CPP /迭代
但是似乎没有计划有 contiguous_iterator_tag (就像我们现在有 random_access_iterator_tag c)报告 std :: iterator_traits< It> :: iterator_category

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.

为什么 contiguous_iterator_tag 缺少?

是否有传统的协议来确定迭代器是否是连续的?
或编译时间测试?

Is there a conventional protocol to determine if an iterator is Contiguous? Or a compile time test?

过去我提到 for containers 如果有 .data()转换为 :: value 类型的指针的成员,并且有 .size()成员可转换为指针差异,然后应该假设容器是连续的,但我不能提取迭代器的类似功能。

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.

一个解决方案对于连续的迭代器,也可以有一个 data 函数。

One solution could be to have also a data function for contiguous iterators.

当然,如果<$ c,连续概念也可以工作$ c>&(it [n])==(&(* it))+ n ,对于所有 n ,但是这个在编译时无法检查。

Of course the Contiguous concept works if &(it[n]) == (&(*it)) + n, for all n, but this can't be checked at compile time.

编辑:我发现这个视频放了这在更广泛的C ++概念环境中。 CppCon 2016:构建和在Patrick Niedzielski的现代多核世界中扩展迭代器层次结构。该解决方案使用概念(Lite),但最后的想法是连续的迭代器应该实现 pointer_from 函数(与我的数据相同(... ) function)。

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).

结论是概念将有助于形式化理论,但它们并不是魔术,在某种意义上说是某个人,某个地方将在相邻的迭代器上定义新的特别命名的函数。
该演讲概括为分段迭代器(具有相应的函数 segment local ),不幸的是它没有说一下有关跨步指针的事情。

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.

推荐答案

理由在 N4284 ,这是连续迭代器提议的采用版本:

The rationale is given in N4284, which is the adopted version of the contiguous iterators proposal:


本文介绍了术语连续迭代器作为随机访问迭代器的改进,没有引入相应的 contiguous_iterator_tag ,在Issaquah讨论Nevin Liber的论文N3884连续迭代器:随机访问迭代器的改进中发现破坏代码。

This paper introduces the term "contiguous iterator" as a refinement of random-access iterator, without introducing a corresponding contiguous_iterator_tag, which was found to break code during the Issaquah discussions of Nevin Liber's paper N3884 "Contiguous Iterators: A Refinement of Random Access Iterators".

有些代码被破坏了,因为它假定 std :: random_access_iterator 无法改进,并对其进行了明确的检查。基本上它打破了不依赖于多态来检查迭代器类别的坏代码,但它破坏了代码,所以从提案中删除了 contiguous_iterator_tag

Some code was broken because it assumed that std::random_access_iterator couldn't be refined, and had explicit checks against it. Basically it broke bad code that didn't rely on polymorphism to check for the categories of iterators, but it broke code nonetheless, so contiguous_iterator_tag was removed from the proposal.

此外, std :: reverse_iterator -like类还有一个问题:反向连续迭代器不能是连续的迭代器,但是一个常规的随机访问迭代器。这个问题本可以在 std :: reverse_iterator 中解决,但更多用户定义的迭代器包装器在复制其迭代器类别时会增加迭代器,否则会说谎或停止正常工作(例如Boost迭代器适配器。)

Also, there was an additional problem with std::reverse_iterator-like classes: a reversed contiguous iterator can't be a contiguous iterator, but a regular random-access iterator. This problem could have been solved for std::reverse_iterator, but more user-defined iterator wrappers that augment an iterator while copying its iterator category would have either lied or stopped working correctly (for example Boost iterator adaptors).

在附注中,有计划带回相当于<$ c的$ c> std :: contiguous_iterator_tag 有一些解决方法可以使它在C ++ 20中集成Ranges TS。

On a side note, there are plans to bring back an equivalent of std::contiguous_iterator_tag with a few workarounds to make it work with the integration of the Ranges TS in C++20.

这篇关于连续迭代器检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆