Hacker News new | ask | show | jobs
by jmaschad 3572 days ago
Wouldn't one want to use 'f(std::forward(x))' with 'auto&&' ?
1 comments

I think the example with function application is a bad one here - moving the element potentially leaves it in an undefined state. It would be a better example if the code did something with x instead of calling a function on it.

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.

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.

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.

I have improved the example in the article to make the code less confusing. Now, a value is assigned to each element in the range and there is no function call.
Thanks, that's clearer.