I want to iterate all possible values of an integer. This code does not work as the termination condition never becomes false:
for (uint32_t i = 0; i <= 0xFFFFFFFF; i++)
std::cout << i << std::endl;
I've come up with this:
auto loopBody = [](uint32_t value)
{
std::cout << value << std::endl;
};
uint32_t last = 0xFFFFFFFF;
for (uint32_t i = 0; i < last; i++)
loopBody(i);
loopBody(last);
It is fairly ugly, though. Is there a prettier way to do this?
I would use something like this:
uint32_t i = 0;
do
{
// Your code here.
i++;
}
while (i != 0);
I personally find it more elegant than a solution involving std::numeric_limits
.
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