3 ms·
> Pass-By-Value: Detect const-ref parameters that would benefit from using the pass-by-value idiom. I don't get why to make this change and the justification (
by asveikau 12y ago
> Pass-By-Value: Detect const-ref parameters that would benefit from using the pass-by-value idiom.
I don't get why to make this change and the justification (after following 2 links deep: Want Speed? Pass by Value) is a 404.
Move constructors are great and it's awesome that a bunch of what used to be copy-constructor overuse in C++03 can now be done without deep copies in C++11. But I don't understand why explicitly move from something that is explicit and guaranteed to be cheap (pass by reference) to something that might be cheap if everything has reasonable move-ctors or (if not all ducks are in a row) might simply degrade to incurring the C++03-style over-copies. Does not seem like a strict improvement to me. Seems instead like blindly applying this to existing code will slow it down in some cases, possibly dramatically.
- StephanTLavavej 12y agoIt's an antipattern. Consider the following scenario: a function says "something = move(passed_by_value);". When called with an lvalue, this assumes that copy construction followed by move assignment is as efficient as copy assignment would be (which is what you get when you overload for const X& and X&&, or perfectly forward). This is untrue for vector/string-like things. When you copy construct a vector/string, it must allocate space for N elements, then copy then. Move assignment just transfers ownership, that part is fast. But consider copy assignment. If the destination has sufficient capacity, NO allocation will be performed - instead, the existing elements will be destroyed and the sufficiently-large buffer reused to hold the new elements. Therefore, "want speed? pass by value" can result in worse performance. Overloading for copy/move is near-optimal and perfect forwarding is optimal - this is what the STL does (push_back is copy/move overloaded, emplace_back perfectly forwards).
- exprL 12y agoThere's certainly no reason to pass something (big) by value if you only need to look at it, but not copy. However, if you're going to copy the object in any case, you should do it right-away so that the caller my move their copy to you if they don't need one any more. I'm guessing this detection is for those cases where the passed-by-ref value is always copied.
- maxlybbert 12y agoMy understanding is simply that many programmers have a habit of passing parameters as const references. If they want a copy of the parameter, they make that copy by hand. In fact, on Thursday, I found a function in the code I work on that does exactly this. That prevents some compiler optimizations ( http://stlab.adobe.com/wiki/images/8/85/2008_06_26_classes_that_work.pdf http://stlab.adobe.com/wiki/images/8/85/2008_06_26_classes_t... ), but those optimizations predate C++11, so I don't know why the advice is in this article. In any case, if you want a function to operate in a copy of a parameter, consider passing that parameter by value instead of passing it by const reference and then making a copy. Note, this is not advice to start making unnecessary copies; instead it's advice that if you need a copy of something, rely on the compiler to make that copy for you.
- rawnlq 12y agoThis post explains when you should and shouldn't use it: https://juanchopanzacpp.wordpress.com/2014/05/11/want-speed-dont-always-pass-by-value/ https://juanchopanzacpp.wordpress.com/2014/05/11/want-speed-...