mercredi 27 février 2019

Integer overflow not occuring : they restart from 0

I tried a simple code and found that integers variables are not overflowing instead it seems that latest C++ compiler has introduced a new functionality related to POD datatypes - if the variable crosses its max value, its values are restart from 0:

#include <iostream>
#include <cstdint>
#include <stdexcept>

int main()
{
try
{
for (uint8_t idx = 254 ;  ; idx++)
{
std::cout << unsigned(idx) << std::endl;
}
}
catch(std::overflow_error e)
{
std::cout << "Error" << std::endl;
}
}

When I run the code the exception code is never executed - but is this the desired behavior?

Aucun commentaire:

Enregistrer un commentaire