Com*_*sMS 6 c++ c++-concepts c++20
C++20 introduces proper Concepts for the different types of iterators in the standard library (input, output, forward, bidirectional, random access, ...).
While the original named requirements for those types did not mention the iterator tags from std::iterator_traits at all, the new C++20 concepts explicitly require them. See for example the input_iterator Concept ([iterator.concept.input]):
template<class I>
concept input_iterator =
input_or_output_iterator<I> &&
indirectly_readable<I> &&
requires { typename ITER_CONCEPT(I); } &&
derived_from<ITER_CONCEPT(I), input_iterator_tag>;
Run Code Online (Sandbox Code Playgroud)
Notice the check for the iterator tag in the last line. All the iterator concepts check for the respective tag like this, except output iterator. Output iterator has always been special in this regard, since the early days of the Ranges TS:
Unlike the output iterator requirements in the C++ standard, OutputIterator in the Ranges TS does not require that the iterator category tag be defined.
What is the reason for this special treatment for output iterators?
在 C++20 中,迭代器类别通常是根据语法自动检测的。显式使用迭代器标签仅用于选择退出或选择加入:
random_access_iterator_tag都用于选择退出更强的迭代器类别(例如,输入迭代器在语法上可能与前向迭代器无法区分);contiguous_iterator_tag用于选择进入contiguous_iterator,因为连续性是一种相对罕见的属性,也无法在语法上检测到。输出迭代器不需要这种细粒度的控制 - 要么你可以写,要么你不能写它,这是从语法上检测到的。
至于何时提供选择加入/选择退出,Casey Carter 解释道:
将参数传递给限制该参数的库组件是一个隐式声明,即该参数要么不满足约束(不满足语法要求),要么对约束建模(满足语法和语义要求)因此通常不需要选择加入/选择退出。
仅当我们希望使库能够假设模型某些概念 X 的参数在满足 Y 时另外模型 X 的某些细化 Y 时,才提供选择加入/选择退出设施。(我一直将其称为“语义提升”) ”,但这不是一个完善的术语。)选择加入还是选择退出取决于误报的预期频率。
“语义提升”通常在非输出迭代器参数上完成。输出迭代器没有什么可提升的。
| 归档时间: |
|
| 查看次数: |
201 次 |
| 最近记录: |