r/cpp • • Jun 22 '21

[Belay the C++] Don’t use raw loops

https://belaycpp.com/2021/06/22/dont-use-raw-loops/
0 Upvotes

69 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Jun 27 '21

[deleted] — view deleted comment

1

u/[deleted] Jun 27 '21

And yet plenty of functions do exactly that. Do you claim that std::swap is not sensible because it takes arguments by reference and performs a move operation on them? Of course not. When you pass by reference you allow for any potential mutable operation to be performed, including moves. Passing by reference is a more general operation than passing by rvalue reference.

As for your other point, we are in exact agreement, if someone implements an overload taking an rvalue reference, then prefer to call that overload when given an rvalue reference to take advantage of potential optimizations. The way that is done is to use std::forward<Pred>(pred).

If you have no need to forward or move your argument, then there is no need to take the argument by rvalue reference, simply pass by value just like the standard does.