Is it undefined behavior to store any value other than zero or one in a `bool` in C++?

22 hours ago 2
ARTICLE AD BOX

I've read the standard says a bool is always either true or false.

Does this mean that storing any other value in a bool is UB?

For example:

#include <cstdint> #include <cstring> #include <print> int main() { bool b = false; uint8_t x = 42; memcpy(&b, &x, 1); if(b) std::println("Hello"); if(!b) std::println("Nope"); return 0; }

Does this program trigger UB?

Read Entire Article