带有模板的 C++ Clang 优化错误

标签 c++ llvm clang++

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

#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 语句更改为 return ret;;此举是不必要的,实际上是一种悲观。

关于带有模板的 C++ Clang 优化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34850695/

相关文章:

c - LLVM 构建,将字符串传递给 LLVMSetValueName C API 时出现问题

c++ - llvm::make_unique 的目的是什么?

clang - C++ 11 代码使用 `clang++` 编译,但不使用 `clang -x c++`

c++ - 类型说明符的组合无效?

c++ - Qt:将对象传递到每个窗口

java - 从 Java 使用 LLVM

c++ - 尝试在 Linux 上使用 Clang++ 编译 c++11 正则表达式教程时出错

c++ - 返回 NULL 作为对象时没有收到任何警告

c++ - cout 可以与 monitor 以外的任何东西相关联吗?

c++ - 如何对成对的 vector 进行排序?