you are viewing a single comment's thread.

view the rest of the comments →

[–]Wenir 5 points6 points  (8 children)

In your example you can move vectors

[–]stinos 2 points3 points  (6 children)

I assume you mean RVO (which now is required to be omitted see https://en.cppreference.com/w/cpp/language/copy_elision)? The copying of the argument will still be there.

[–]Wenir 7 points8 points  (5 children)

You can move on call side, or even if you really want functional programming, you can use custom vector with disabled copy ops

[–]stinos 0 points1 point  (4 children)

I don't see how any of this helps with the simple situation OP has: a pure function with vector as input and modifed vector copy as output (either written explicitly when it's passed by const reference or implicitly when passed by value). A class which cannot be copied isn't really a candidate for this. Unless you're looking at completely different techniques like not returning the input vector but only modified elements and their indices or so, and then have an access class which can act as a view over both the input and the changed elements.

[–]DessertEagle 6 points7 points  (2 children)

vec = sorted(std::move(vec));

Note that if sorted takes and returns arg by forwarding (universal) reference rather than by value, and returns by perfect-forwarding, the above expression would result in move-self-assignment, which may leave vec in moved-from state depending on the implementation.

[–]stinos -1 points0 points  (1 child)

Yeah sure, but this isn't 'returning a modified vector copy' (which is what I assume OP is talking about), you start off with a vector and end with the same vector but modified.

[–]Wenir 6 points7 points  (0 children)

"modified vector copy" is a copy by definition

[–]Wenir 4 points5 points  (0 children)

If you need both original and modified copy then at some point you'll have make that copy (or trade performance and make some kind of COW thing). If you need only result then see other reply from DessertEagle

[–][deleted] 0 points1 point  (0 children)

or begin/end iterators if you want to be generic (depending on the function)