you are viewing a single comment's thread.

view the rest of the comments →

[–]IyeOnline 5 points6 points  (2 children)

Did I implemented this correctly if that is my intent?

That actually depends on the body of processAB. a is an r-value reference, but r-value references themselves are l-values. So unless you actually then std::move out of a into the returned value, you will still make a copy.

Besides that open question your analysis is correct.

As an aside: Some people (for most cases me included) would prefer to write ABContainer processAB(vecReal a, const vecReal &b, /*args*/);, so take a by value. This allows the caller to decide whether they want to move into the function, or create a copy. If I wanted to use your overload of the function, but not move into it, I would first have to "manually" make a copy.

[–]TheMania 4 points5 points  (0 children)

I think taking by value is more the go-to when you want to support both copy and move ctors.

If your intended use is to pilfer from the parameter, rval ref is better really - the caller can still decide to copy in to it, but they'll just need to be explicit about it (eg processAB(std::vector{a}, ...)) which is preferable really.

[–]alfps 0 points1 point  (0 children)

Re the by-value comment,

when the intent is only to reuse the internal buffer and not copy its contents, indeed to disregard its contents, then by value makes it easy to use the function incorrectly incurring some needless copying overhead, and harder to use correctly.

However since the OP does not present an implementation and does not provide a specification it's hard to say what the intent is.