Two simplified examples:
#include <cstdlib>
#include <string>
#include <vector>
class Object{};
void use1(Object * o)
{
(void)(o);
}
void use2(std::string & s)
{
(void)(s);
}
int f1()
{
Object * object_ptr{ nullptr };
{
Object object{};
object_ptr = &object;
}
use1(object_ptr); // UB
return rand();
}
int f2()
{
std::vector<std::string> v{"foo", "bar"};
auto & v_ref = v[0];
v.emplace_back("baz");
use2(v_ref); // UB
return rand();
}
int main()
{
return f1() + f2();
}
(rand()
is just for testing.)
Rust just can not compile sources like this. With Clang or GCC (or maybe MSVC?) is there an option to detect such an undefined behavior?