<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Optimization bug with member function return a rvalue in a class template"
   href="https://llvm.org/bugs/show_bug.cgi?id=26189">26189</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Optimization bug with member function return a rvalue in a class template
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.7
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>waistcoatwm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I have a simple piece of code as follows:

#include <map>
#include <iostream>

template <typename LocType, typename Base>
class MapWrapper {
public:
  Base&& get_and_erase(LocType x) {
    Base ret = std::move(_data[x]);
    _data.erase(x);
    // Uncomment the cout will give correct result
    // std::cout << "retval = " << ret << std::endl;
    return std::move(ret);
  }
  void increase(const LocType& x, const Base& w) {
    if (w == 0.0) {
      return;
    }
    _data[x] += w;
  }
private:
  std::map<LocType, Base> _data;
};

int main() {
  MapWrapper<int, double> a;
  a.increase(1, 1.0);
  double w = a.get_and_erase(1);
  std::cout << "w = " << w << std::endl;
  return 0;
}

I think the output should be 1, but when I use my MAC with

Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

and compile it with: g++ --std=c++11 -O2 debug.cpp -o debug

What I get is : w = 2.64619e-260

The only way I can make it correct is to turn off -O2 or force an output by
uncomment the `std::cout` in the code.

Any ideas?</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>