title: C++: rvalue references used in ternary operator seem to break existing code
body: <p>I'm currently porting one of my projects, which has been developed for years using Borland C++-Builder 5 and 6, to the most current Embarcadero C++-Builder XE 3 Update 2. XE 3 supports some of the new C++11-things like rvalue references which is of course completely new to me, due to the former used very old compilers. I needed only very few modifications to get my project compilable, but during runtime I'm facing one problem which seems to be a result of the new rvalue references and move semantics.</p>
  
  <p>I have a class with a field of type std::wstring storing a path which is only read from one method using this field in a ternary operator like the following:</p>
  
  <pre><code>std::wstring retVal = someCondition ? this-&gt;classField : Util::doSomething(someArg);
  </code></pre>
  
  <p>someCondition is just a call to std::wstring.empty() and Util::doSomething returns std::wstring as a value, no reference or else involved, just copied data.</p>
  
  <p>With XE 3, after the first time someCondition evaluated to true, retVal gets properly filled with the content of classField, but afterwards classField's content is empty. Using the debugger I could follow execution to an optimized assignment operator whith rvalue references:</p>
  
  <pre><code> #if _HAS_RVALUE_REFERENCES
  [...]
      _Myt&amp; operator=(_Myt&amp;&amp; _Right)
          {   // assign by moving _Right
          return (assign(_STD forward&lt;_Myt&gt;(_Right)));
          }
  
      _Myt&amp; assign(_Myt&amp;&amp; _Right)
          {   // assign by moving _Right
  [...]
  </code></pre>
  
  <p>What I've read about rvalue references would perfectly explain my problem, I even found two comments explaining why my classField is treated as an rvalue.</p>
  
  <p><a href="https://stackoverflow.com/a/8535301/2055163">https://stackoverflow.com/a/8535301/2055163</a></p>
  
  <p><a href="https://stackoverflow.com/a/6957421">https://stackoverflow.com/a/6957421</a></p>
  
  <p>I can fix the line above with one additional manual copy of classField:</p>
  
  <pre><code>std::wstring retVal = someCondition ? std::wstring(this-&gt;classField) : Util::doSomething(someArg);
  </code></pre>
  
  <p>But this doesn't solve the problem that I need to review each and every usage of a ternary operator (where lvalues and rvalues are mixed) in each project I want to port without any help from the compiler, because it's a runtime problem that may or may not occur.</p>
  
  <p>What I don't understand is if there's something wrong or bad practice with my usage of the ternary operator? Are there better solutions to detect those cases? Why should I manually copy some objects just to trick optimizations? Is this really intended behavior and no problems in most of your codebase out there? How do you deal with such problems?</p>
  
  <p>I'm a bit confused on how to progress right now and would really appreciate any suggestions and/or explanations. Thanks!</p>
  
  <hr>
  
  <p>I tested the following two cases which worked:</p>
  
  <p><a href="http://ideone.com/mWxxK3" rel="nofollow noreferrer">http://ideone.com/mWxxK3</a></p>
  
  <p>The first example looks similar to my case, expect that no class instances are used to store the global string. dummy2 is properly filled and dummy keeps its content.</p>
  
  <pre><code>std::wstring retVal = someCondition ? this-&gt;classField : doSomethingResult;
  </code></pre>
  
  <p>When I change my line above to remove the rvalue by saving the result of Util:... to a std::wstring before using the ternary operator, everything works as expected. retVal has its content and this->classField keeps its, too.</p>
  
  <p>What's the conclusion now? :-/</p>