The following code exhibits undefined behaviour:
#include <memory>
void consumer(std::string && str){}
int main(){
std::string str = "foo";
consumer(std::move(str));
str = "bar"; //<- UB
}
How can I catch this? There doesn't appear to be any compiler warnings in gcc
or clang
.
I also tried using the undefined behaviour sanitizer:
clang++-3.9 -std=c++1z -g -Weverything -fsanitize=undefined -o main main.cpp
It did not catch anything.
Are there reliable ways to catch these problems?