C++ 模板的 Clang 优化错误

C++ Clang optimization bug with template

本文关键字:优化 错误 Clang C++      更新时间:2023-10-16

我有一段简单的代码,如下所示:

#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;
}

我认为输出应该是 1。它在g ++ 4.8.2中工作正常,但是当我使用MAC时

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

并编译为:

g++ --std=c++11 -O2 debug.cpp -o debug

我得到的是:

w = 2.64619e-260

我能使其正确的唯一方法是关闭-O2或通过取消注释代码中的std::cout来强制输出。

有什么想法吗?

您的代码具有未定义的行为。 get_and_erase返回对局部变量的引用。启用优化会暴露此错误。您的显式std::move欺骗了编译器通常会发出的返回对局部变量的引用的警告。

要解决此问题,请将返回类型更改为 Base ,将 return 语句更改为 just return ret; ;此举是不必要的,实际上是一种悲观。