Re: Proposed idiom for efficient use of move semantics
 
On Nov 19, 3:34 am, "Jeffrey Yasskin" <jyass...@gmail.com> wrote:
However, when fill() is also useful outside of loops,
we have to either use the less convenient and less const form:
   string s;
   fill(&s);
or write a boilerplate overload:
   string fill() {
     string result;
     fill(&result);
     return result;
   }
   const string s = fill();
I find the boilerplate solution perfectly acceptable. (Perhaps even
more so when we have move semantics, as a fall-back where the NRVO
isn't possible (is that ever the case? I don't know)).
Rvalue references and move semantics can potentially save on copies
here, but they don't immediately save allocations. But consider the
following definition of fill() instead:
<implementation snipped>
   string s;
   for (int i = 0; i < big_number; ++i) {
     s = fill(move(s));
     use(s);
   }
I find that syntax non-obvious. Move from s, but only so that the
storage can be reused (where possible)? Hmm. Also, I can't think of a
common use-case where you'd pass a genuine rvalue into fill. The rvalue
ref seems misplaced to me.
I would much prefer just
     for (int i = 0; i < big_number; ++i) {
       fill(s); // use pointers if you prefer
       use(s);
     }
That is, the implementor of fill provides
   void fill(string&); // [1]
   string fill();      // [2]
(the implementation of the [1] should start by clearing s, and [2]
calls [1])
James
-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]