I mistakenly have dereferenced an uninitialized std::optional
and only found out using valgrind (Syscall param exit_group(status) contains uninitialised byte(s)
), like in this minimal working example:
#include<optional>
struct Foo {
int i=1;
};
std::optional<Foo> bar(){
return {};
}
int main() {
return bar()->i;
}
What can protect me from these bugs?
Is there some macro/flag that I can define before #include<optional>
in order to have at least a runtime error (make std::optional
throw on every bad access or assert(this->has_value())
)? I don't care about performance here and prefer defensive programming.