How to use structure as operand of conditional?

I have a simple structure in C ++ 11

struct a { int a; int b; int c; .... } 

I would like to use this structure as if it were a scalar type, so I overloaded all the operators.

One behavior that I cannot find how to determine is the use of structure in an if statement:

 av = {1,2,3}; if (v) { } 

Is there such an operator that I can overload to enable this behavior? I want a standard behavior: if any bit is 1 in the structure, this is true, otherwise it is false.

+5
source share
1 answer

Add an explicit boolean conversion:

 struct a { explicit operator bool() const { return a || b || c; } int a; int b; int c; // ... }; 
+16
source

Source: https://habr.com/ru/post/1200913/


All Articles