简体   繁体   中英

Must an input iterator have a const dereference operator?

输入迭代器是否必须具有const解引用运算符,还是仅非const版本也可以满足其要求?

No.

For example the "Input Iterator".

This is usually read from a stream. De-referencing it reads a value and changes the underlying stream. Using the de-reference operator is not valid again until you use the ++ operator.

http://www.sgi.com/tech/stl/InputIterator.html

Edit based on comment.

Are you asking can?

Sure it can you can do anything in C++.

Or are you asking should?

Now it depends on which bit is const:

  • Do you mean should de-reference by a const method?.
  • Or do you mean the reference returned (to the object) is const?

Personally I think the answer to both these questions is no.
I think am pretty sure that this does not violate the constraints of the input iterator (which are defined in the link above).

BUT I think it is unwise to return a non cost reference to an object that if mutated by the user will not mutate the underlying object in what the stream is referring too (this violates the principle of least surprise).

Overall I think you need to clarify your question.
If you explain the context in more detail and ask weather a particular signature violates constraints then I may be able to give a better answer. Currently the question is just overly vague thus I can only express the answer in an overly general fashion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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