Re:Auto Type Deduction in C++ Range-Based For Loops
by sseagull on Friday September 02, 2016 @10:47AM
(#12413365)
This has been slightly confusing to me, but the syntax auto&& might not mean an rvalue reference. According to the post, it means a universal reference. I wish they would have made a different syntax. For example, with templates, my understanding is:
void somefunction(int && i) // i is an rvalue reference
template<typename T> void somefunction2(T && i) // i is a universal reference
It might be a similar thing with auto&&. So I would tentatively agree that std::forward should be used.
Re:Auto Type Deduction in C++ Range-Based For Loops
by lorenzhs on Friday September 02, 2016 @11:32AM
(#12413706)
Yes in this case it's a universal reference, I'm aware. Universal references work with auto&&. What std::forward does is the following:
- if you put in an rvalue reference, it's like std::move
- if you put in anything else, it does nothing.
So my point about the article's formulation ("when you want to modify elements in the range in generic code") and its example remains.
So I don't think you'd want to forward it. Note that you also need the element's type to forward it, which isn't possible in general with the example's signature.