Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The basic problem is that return by value <em>might</em> copy. The C++ implementation is not required by the standard to apply copy-elision where it does apply. That's why the object still has to be copyable: so that the implementation's decision when to use it doesn't affect whether the code is well-formed.</p> <p>Anyway, it doesn't necessarily apply to every copy that the user might like it to. For example there is no elision of copy assignment.</p> <p>I think your options are:</p> <ul> <li>implement a proper copy. If someone ends up with a slow program due to copying it then their profiler will tell them, you don't have to make it your job to stop them if you don't want to.</li> <li>implement a proper move, but no copy (C++11 only).</li> <li>change <code>getFoo</code> to take a <code>Foo&amp;</code> (or maybe <code>Foo*</code>) parameter, and avoid a copy by somehow mutating their object. An efficient <code>swap</code> would come in handy for that. This is fairly pointless if <code>getFoo</code> really returns a default-constructed <code>Foo</code> as in your example, since the caller needs to construct a <code>Foo</code> before they call <code>getFoo</code>.</li> <li>return a dynamically-allocated <code>Foo</code> wrapped in a smart pointer: either <code>auto_ptr</code> or <code>unique_ptr</code>. Functions defined to create an object and transfer sole ownership to their caller <em>should not</em> return <code>shared_ptr</code> since it has no <code>release()</code> function.</li> <li>provide a copy constructor but make it blow up somehow (fail to link, abort, throw an exception) if it's ever used. The problems with this are (1) it's doomed to fail but the compiler says nothing, (2) you're enforcing quality of implementation, so your class doesn't work if someone deliberately disables RVO for whatever reason.</li> </ul> <p>I may have missed some.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload