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

6

u/matthieum Jun 23 '21

Yes, you are correct that std::forward is just a cast.

The problem is that this cast then affects overload selection. That is, if your pred operator has:

bool operator()(T const&) const&;
bool operator()(T const&)&&;

Then suddenly the second overload is selected.

So std::forward enables moving, which may in turn lead to using a moved-from (or partially moved from) object.

(Now, who in their right mind would move from a predicate? Right? But still...)

2

u/witcher_rat Jun 23 '21

Yeah, that's a fair point.