[英]Requirements for forward iterator iterator_traits::reference
I'm trying to figure out exactly what requirements are made on forward_iterators' reference
types. 我试图弄清楚对forward_iterators的
reference
类型有什么要求。 In the obvious cases you'll have value_type = T;
在明显的情况下,您将拥有
value_type = T;
and reference = T&;
和
reference = T&;
. 。 Reading the cppreference page on forward iterator requirements, I saw
阅读有关正向迭代器要求的cppreference页面 ,我看到了
Expression Return Equivalent expression
*i++ reference value_type& temp=*i; ++i; return temp;
std::vector<bool>
shows that the "equivalent expression" isn't always valid since it returns a proxy object: std::vector<bool>
显示“等效表达式”并不总是有效,因为它返回了代理对象:
std::vector<bool> v(10);
auto i = v.begin();
std::vector<bool>::iterator::value_type& temp = *i; // error
// can't bind bool& to std::_Bit_reference
The equivalent expression isn't mentioned in the standard that I saw. 我看到的标准中没有提到等效表达式。 The proxy object allows assignment though, which might be the key to conformance.
但是,代理对象允许分配,这可能是一致性的关键。
Outside of just all around trying to nail down the requirements, my specific question concerns knowing whether or not having value_type
== reference
where neither is a reference or supports assignment, would work with the standard libraries. 除了尽力确定需求之外,我的具体问题还涉及到知道是否具有
value_type
== reference
(既不是引用也不支持赋值)将与标准库一起使用。
Would some Container<int>
with an iterator
tagged as forward_iterator_tag
and reference == int
be valid? 某些带有
iterator
标记为forward_iterator_tag
并且reference == int
Container<int>
是否有效?
The requirements are enumerated in [forward.iterators]: [forward.iterators]中列举了这些要求:
A class or pointer type
X
satisfies the requirements of a forward iterator if如果满足以下条件,则类或指针类型
X
满足前向迭代器的要求:
X
satisfies the requirements of an input iterator (24.2.3),X
满足输入迭代器(24.2.3)的要求,X
satisfies the DefaultConstructible requirements (17.6.3.1),X
满足DefaultConstructible要求(17.6.3.1),- if
X
is a mutable iterator,reference
is a reference toT
;如果
X
是一个可变迭代器,则reference
是对T
的引用; ifX
is a const iterator, reference is a reference toconst T
,如果
X
是const迭代器,则reference是对const T
的引用。- [...]
[...]
So if your container has reference
== int
, then it does not meet the requirements of forward iterator. 因此,如果您的容器具有
reference
== int
,则它不满足正向迭代器的要求。 Which I suppose technically makes vector<bool>::iterator
just an input iterator, even though it's tagged as a random access iterator. 从技术上讲,我认为
vector<bool>::iterator
只是一个输入迭代器,即使它被标记为随机访问迭代器也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.