Answer by Kevin for Can I check a small array of bools in one go?
Several answers have already explained good alternatives, particularly std::bitset and std::any_of(). I am writing separately to point out that, unless you know something we don't, it is not safe to...
View ArticleAnswer by Justin Time - Reinstate Monica for Can I check a small array of...
...And for the obligatory "roll your own" answer, we can provide a simple "or"-like function for any array bool[N], like so:template<size_t N>constexpr bool or_all(const bool (&bs)[N]) { for...
View ArticleAnswer by Oblivion for Can I check a small array of bools in one go?
You can use std::bitset<N>::any:Any returns true if any of the bits are set to true, otherwise false.#include <iostream> #include <bitset> int main (){ std::bitset<4> foo; //...
View ArticleAnswer by Jesper Juhl for Can I check a small array of bools in one go?
The standard library has what you need in the form of the std::all_of, std::any_of, std::none_of algorithms.
View ArticleAnswer by Yksisarvinen for Can I check a small array of bools in one go?
As πάνταῥεῖ noticed in comments, std::bitset is probably the best way to deal with that in UB-free manner.std::bitset<4> boolArray {};if(boolArray.any()) { //do the thing}If you want to stick to...
View ArticleCan I check a small array of bools in one go?
There was a similar question here, but the user in that question seemed to have a much larger array, or vector. If I have:bool boolArray[4];And I want to check if all elements are false, I can check [...
View Article