I have a simple struct in C++11
struct a {
int a;
int b;
int c;
....
}
I would like to use this struct as if it is an scalar type itself, so I overloaded all operators.
One behaviour I can't find how to define is the use of a struct in an if statement:
a v = {1,2,3};
if (v) { }
Is there an operator that I can overload to enable this behaviour? I want the standard behaviour: if any bit is 1 in the struct it's true, else it's false.
Add an explicit boolean conversion:
struct a
{
explicit operator bool() const
{
return a || b || c;
}
int a;
int b;
int c;
// ...
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With