10

In pre-Range TS code, I might do something like the following to get a (potentially modifiable) reference to a ForwardIterator's value:

auto &val = *it;

This would also be used in range-based for loops over such iterators:

for(auto &val : some_range)

However, in C++20 and Range TS-based code, I'm seeing a lot of auto&& usage in these locations. I understand what auto&& is doing from a language standpoint. What I don't understand is why it's being used in these places, when auto& ought to work just fine? Most code of this nature isn't forwarding the reference, so why is it using a forwarding reference to capture it?

1 Answer 1

14

This idiom is being used because the Range TS and the C++20-equivalent actually requires this idiom. The reason for this requirement has to do with the fact that proxy iterators are now legal constructs for all categories of iterators.

A proxy iterator's operator* does not return a value_type&; it returns a prvalue of an object that acts like an reference. Therefore, if you tried to store a reference to the value with auto&, your code would fail to compile with a proxy iterator.

The Range TS's iterator concepts never require that the return value from operator* is actually a language reference. The Readable concept only requires that the return value is convertible to a value_type. The Writable concept only requires that a value_type can be assigned to the return value.

So code getting references from iterators of all kinds under the Range feature must now use auto&&, which can bind to prvalues without making them const. So when dealing with ranges, if you want to get a possibly non-const reference to the value, auto&& is the most convenient way. If you want just a const reference, you can continue to use const auto &.


Note that auto&& also handles iterators that directly return prvalues of type value_type from their operator*. Obviously these are only useful for Readable iterators, but they're quite useful. The simplest example would be an iterator for a counting range over some number of integers. The iterator returns an int, not a reference to some memory. Such iterators can be RandomAccessIterators in the Range TS due to not having a hard requirement for returning a language reference.

Such iterators would also make sense for regular expression matching. The current regex_iterator returns a reference to an internal object; that's necessary for being a ForwardIterator. Instead, it could have been much smaller by having operator* do the search and return the match object as a prvalue. But the old system wouldn't allow that.

Given such an iterator, auto& cannot be used to capture "references" to their results.

8
  • 1
    This might have been a good idea in C++11 to C++17 anyway: "output iterator" never did require reference to be an actual reference (though "forward iterator" did). So it's useful for an algorithm that would otherwise work with output iterators. And since it's more general, it's easier to just make auto&& a habit rather than remembering when to use which.
    – aschepler
    Commented Jul 22, 2018 at 1:30
  • Technically, Ranges are still only a "potential C++20" feature - the proposal is on track, but hasn't yet been merged into the working draft. "except for contiguous, but the Range TS and C++20 feature doesn't define that iterator category" - LEWG approved P0944 "Contiguous Ranges" in Rapperswil, it's now a part of the omnibus proposal that's in LWG. "it returns a prvalue of an object that acts like an iterator" You probably meant to say "reference" here.
    – Casey
    Commented Jul 22, 2018 at 1:35
  • 3
    Last but not least, I don't consider iterators whose reference operator returns a prvalue of the iterator's value type to be "proxy iterators." I prefer to refer to that class as "prvalue iterators." They also require using auto&& or const auto& to bind to the result of a dereference, but don't involve the other machinery (iter_move, iter_swap) that "true" proxy iterators require to workaround the lack of support for "smart references" in C++.
    – Casey
    Commented Jul 22, 2018 at 1:38
  • 2
    Oh: and I should also point out that it's not strictly necessary to use a forwarding reference. Given an iterator it, std::iter_reference_t<decltype(it)> foo = *it; is always valid. So even the heathens who hate auto can use Ranges ;)
    – Casey
    Commented Jul 22, 2018 at 1:46
  • @Casey: P0944 is useful, but I find it highly annoying that the data customization point can be used to get a pointer to the beginning of a contiguous range, but you cannot get a pointer to a contiguous iterator position at all. Not unless it is dereferencable, and the principle purpose of iterator-to-pointer conversion is to find a way to do it without needing the iterator to be dereferenceable. Also, why aren't ContiguousRanges required to be SizedRanges? Commented Jul 22, 2018 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.