Following up my own question at Deduce type of template type in C++ some people mentioned passing iterators
by reference is not idiomatic and a given use case in which it would not work:
template <typename Iter> iterate(Iter &first, Iter &last)
{
// Do something with the iterators
}
iterate(container.begin(), container.end()); // error, binding non-const ref to rvalue!
Looking deeper I found (at least) two other topics which cover the former:
- What's wrong with passing C++ iterator by reference?
- Why do C++ STL container begin and end functions return iterators by value rather than by constant reference?
But there seems to be no question/answer as to whether passing the iterators by rvalue reference && would be (even) better than passing them by value. As in
template <typename Iter> iterate(Iter &&first, Iter &&last)
{
// Do something with the iterators
}
iterate(container.begin(), container.end());
My code has compiled and run fine using rvalue references and hence my thoughts about this.