title: Why are C++ iterators required to return a reference?
body: <p>I'm implementing an iterator that iterates over the results of a generator function rather than over a data structure in memory such as a vector or map.</p>
  
  <p>Reading through the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf" rel="noreferrer">final working draft for C++17</a> §27.2.3, the return type of the dereference operator for <strike>an input iterator (and by extension, most other iterators)</strike> <a href="https://stackoverflow.com/a/46626887/5729206"><em>a forward iterator</em></a> is required to be a reference. This is fine for items that exist in the data structure the iterator is iterating over. However, because I'm not using a data structure and am calculating each item when the dereference operator is called, I don't have a valid reference to return; the calculated item is destroyed when the operator returns. To work around this, I am storing the result of the calculation in the iterator itself and returning a reference to the stored result. This works fine for my use case, but has issues of its own when used with arbitrary user-defined types.</p>
  
  <p>I can understand iterators being <em>allowed</em> to return a reference, but why would this be a requirement for non-mutating iterators? Did the writers of the standard not consider generators and on-the-fly transformations to be valid use cases for iterators? Would returning a value instead of a const reference cause any actual harm?</p>
  
  <p>[edit]: I'm asking more out of curiosity about <em>why</em> the standard is written the way it is, since I already have a perfectly good workaround.</p>