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. It works fine in g++ 4.8.2, 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?