重新使用移动的数据时,如何捕获未定义的行为?

时间:2016-06-30 04:39:54

标签: c++ move compiler-warnings static-analysis undefined-behavior

以下代码显示未定义的行为:

#include <memory>

void consumer(std::string && str){}

int main(){

  std::string str = "foo";
  consumer(std::move(str));
  str = "bar"; //<- UB
}

我怎么能抓到这个? gccclang中似乎没有任何编译器警告。

我也尝试使用未定义的行为消毒剂:

clang++-3.9 -std=c++1z -g -Weverything -fsanitize=undefined  -o main main.cpp  

没有抓到任何东西 是否有可靠的方法来解决这些问题?

0 个答案:

没有答案