How can I pass my object to an if
statement and check if it is true
or false
example:
struct A {
int num;
operator bool good() const {return num > 0;}
}
A a{1}; if(a) { /* it's all good man! */ }
I got inspired by std::function
where you can pass it into a if statement and it will return true if function pointer is set:
void checkFunc( std::function<void()> const &func )
{
// Use operator bool to determine if callable target is available.
if( func )
{
std::cout << "Function is not empty! Calling function.\n";
func();
}
else
{
std::cout << "Function is empty. Nothing to do.\n";
}
}