Edit: I have completely changed this answer as I have been told the previous answer could lead to problems on some compiler settings.
The way you do this is by creating a pointer to member array of all struct members. This way you can easily loop through them all. Of course this isn't ideal because when you have a lot of members in a struct it can get quite long so I would suggest using a boolean map but if you really want a struct you do it like this.
First create a global pointer to member array of all members:
constexpr bool yourstruct::* arrName[<yourNumberOfElements>] = {
&yourstruct::elem1,
&yourstruct::elem2,
&yourstruct::elem3,
// continue until you have all your members here
};
Then you can create a function like this:
bool isAtLeastOneTrue(struct yourstruct* p1, int size){
for(int i = 0; i<size;i++){
if(p1->*arrName[i])
return true;
}
return false;
}
And then you can call the function to see if any member is true.
isAtLeastOneTrue(&yourstructInstance, <yourStructSize>)
Here is some an example of how to use this
struct test{
test(){
a1 = false;
a2 = false;
a3 = false;
a4 = false;
a5 = false;
a6 = false;
a7 = false;
a8 = false;
a9 = true;
a10 = false;
a11 = false;
}
bool a1;
bool a2;
bool a3;
bool a4;
bool a5;
bool a6;
bool a7;
bool a8;
bool a9;
bool a10;
bool a11;
};
constexpr bool test::* ptma[11] = { // pointer to member array
&test::a1,
&test::a2,
&test::a3,
&test::a4,
&test::a5,
&test::a6,
&test::a7,
&test::a8,
&test::a9,
&test::a10,
&test::a11
};
bool isAtLeastOneTrue(struct test* p1, int size){
for(int i = 0; i<size;i++){
if(p1->*ptma[i])
return true;
}
return false;
}
int main() {
test a;
cout << isAtLeastOneTrue(&a, 11) << endl;
}