[PATCH] cpp11-migrate: Add a transform that use pass-by-value in constructors

David Blaikie dblaikie at gmail.com
Sat Aug 10 09:20:30 PDT 2013


  While constructors are an interesting case - do you intend to/is there any reason this hasn't been generalized over arbitrary functions that involve taking exactly one copy of a parameter & otherwise not referencing that parameter?

  & it's clear that this is an unsafe transformation, right? If, in the original example, the parameter object is referenced indirectly elsewhere (eg: another reference parameter has an indirect reference to the movable parameter, or the movable parameter is a reference to a global) causing the copy to happen at the time the argument was passed, rather than when the original code copied it could cause an observably different object to be copied. I don't think that negates the value in having this transformation - just something to be aware of.

  Also the "pass by value" approach here isn't optimal but is the nicest/simplest way to write this - in the case where the caller passes a non-temporary there will be one copy construction + one move construction when pass by value is used. In the original code (pass by const ref) there will be one copy construction and no move construction. So the really efficient way is to have two functions - one for rvalue ref (that move constructs) and one for const ref (that copy constructs). But that results in a ridiculous explosion of functions that's just silly - making the pass by value quite sensible as a default until you find some reason to need to split the two functions (because the cost of the extra move is unacceptable in some particular case for some reason)

  (of course, as an extension - detecting read-only operations on the movable object & replacing references to the parameter with references to the copy (if it hasn't been mutated) could be cool too, but much more work I assume)

http://llvm-reviews.chandlerc.com/D1342



More information about the cfe-commits mailing list