[llvm-bugs] [Bug 26189] New: Optimization bug with member function return a rvalue in a class template

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Jan 18 01:07:10 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=26189

            Bug ID: 26189
           Summary: Optimization bug with member function return a rvalue
                    in a class template
           Product: clang
           Version: 3.7
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: waistcoatwm at gmail.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

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?

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160118/d1226ec5/attachment-0001.html>


More information about the llvm-bugs mailing list