J
Jason Heyes
I would like to be able to extract an integer from a stream without having
to write a test when I want the integer within some range. Unfortunately
there is no range-checked integer type in the standard C++ library. Consider
the following function:
std::istream &read_range_checked_integer(std::istream &is, int
&result_value)
{
int value;
if (!(is >> value))
{
std::cout << "failed to extract integer" << std::endl;
return is;
}
if (!(value >= 0 && value < 64))
{
std::cout << "extracted integer is out of range" << std::endl;
is.setstate(std::ios_base::failbit);
return is;
}
result_value = value;
return is;
}
I want a type to help write read_range_checked_integer more succinctly. For
example:
std::istream &read_range_checked_integer(std::istream &is, int
&result_value)
{
range_checked_integer<0, 64> value;
if (!(is >> value))
{
std::cout << "failed to extract range_checked_integer<0, 64>" <<
std::endl;
return is;
}
result_value = value.get_int();
return is;
}
Can somebody help me find a type that behaves like range_checked_integer?
Thanks alot.
to write a test when I want the integer within some range. Unfortunately
there is no range-checked integer type in the standard C++ library. Consider
the following function:
std::istream &read_range_checked_integer(std::istream &is, int
&result_value)
{
int value;
if (!(is >> value))
{
std::cout << "failed to extract integer" << std::endl;
return is;
}
if (!(value >= 0 && value < 64))
{
std::cout << "extracted integer is out of range" << std::endl;
is.setstate(std::ios_base::failbit);
return is;
}
result_value = value;
return is;
}
I want a type to help write read_range_checked_integer more succinctly. For
example:
std::istream &read_range_checked_integer(std::istream &is, int
&result_value)
{
range_checked_integer<0, 64> value;
if (!(is >> value))
{
std::cout << "failed to extract range_checked_integer<0, 64>" <<
std::endl;
return is;
}
result_value = value.get_int();
return is;
}
Can somebody help me find a type that behaves like range_checked_integer?
Thanks alot.