Consider this piece of code:
struct A {
bool f() { return true; }
};
// ...
std::vector<A*> v = {new A(), nullptr, new A()};
std::find_if(v.begin(), v.end(), [](const A* object) { return object->f(); });
If we invoke a method from nullptr
object, is this an undefined behaviour? If yes and I want to resolve this issue, will it suffice to add A != nullptr
in the beginning, so that the condition short-circuits before the potentially dangerous code would be executed?