简体   繁体   English

当 std::move_iterator 取消引用右值引用时,为什么它可以将自己宣传为前向(或更强)迭代器?

[英]Why can std::move_iterator advertise itself as a forward (or stronger) iterator, when it dereferences to an rvalue reference?

According to cppreference, std::move_iterator sets its ::iterator_category to the category of its underlying iterator 1 .根据 cppreference, std::move_iterator将其::iterator_category设置为其底层迭代器1的类别。

But I reckon it can be an input/output iterator at best, since for forward iterators reference must be an lvalue reference, while move_iterator sets reference (and the return type of operator* ) to an rvalue reference 2 .但我认为它充其量只能是一个输入/输出迭代器,因为对于前向迭代器reference必须是一个左值引用,而move_iteratorreference (以及operator*的返回类型)设置为一个右值引用2

Is this a blatant mistagging of the iterator with a wrong category?这是一个错误类别的迭代器的公然错误标记吗?

Being able to do this for my own iterators is undoubtedly convenient.能够为我自己的迭代器做到这一点无疑是很方便的。 Is there any reason I shouldn't do this, if even the standard library does so?如果即使标准库也这样做,我有什么理由不这样做吗?


1 But anything stronger than random_access_iterator_tag is truncated to random_access_iterator_tag , which is weird, since contiguous_iterator_tag is only supposed to be used for ::iterator_concept . 1但是任何比random_access_iterator_tag更强的东西都会被截断为random_access_iterator_tag ,这很奇怪,因为contiguous_iterator_tag只应该用于::iterator_concept

2 Or leaves it untouched if it's not a reference, but then the underlying iterator shouldn't advertise itself as a forward iterator either. 2或者如果它不是引用,则保持不变,但是底层迭代器也不应该将自己宣传为前向迭代器。

Cppreference article on forward iterator requirements was wrong (already fixed by someone).关于前向迭代器要求的 Cppreference 文章是错误的(已由某人修复)。 reference must be any reference ( & or && ), not specifically lvalue reference ( & ). reference必须是任何引用( &&& ),而不是具体的左值引用( & )。 Meaning move_iterator does conform.含义move_iterator确实符合。

See [forward.iterators]/1.3 :请参阅[forward.iterators]/1.3

if X is a mutable iterator, reference is a reference to T ;如果X是可变迭代器,则reference是对T的引用; if X is a constant iterator, reference is a reference to const T如果X是常量迭代器,则reference是对const T的引用

See also LWG1211 (from 2009), which raised the same issue, and was resolved by N3066 (in 2010), which changed the wording from "lvalue reference" to "any reference".另请参阅LWG1211 (从 2009 年开始),它提出了同样的问题,并由N3066 (2010 年)解决,它将措辞从“左值引用”更改为“任何引用”。 (Thanks @康桓瑋 for the links). (感谢@康桓玮的链接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM