Another technical problem trying to overcome the repetion of values 10 and 20
Code:
template <class T>
class ValidateRangeT
{
T lower, upper;
public:
ValidateRangeT(const T& lower, const T& upper): lower(lower), upper(upper) {}
bool operator () (const T& value) const
{
return value > lower && value < upper;
}
};
template <class T>
ValidateRangeT<T> ValidateRange(const T& low, const T& high)
{
return ValidateRangeT<T>(low, high);
}
int main()
{
int x;
int low = 10;
int high = 20;
Input::Read<int, std::string>(
x,
"Enter an integer: ",
"That is not a valid integer. Try again.",
boost::str(boost::format("The integer must be bigger than %d and lower than %d.") % low % high),
ValidateRange(low, high)
);
std::cout << "You entered: " << x << std::endl;
}
I have to go to some lengths because you assume that all the messages will be of the same type. (This may also be the reason why you "needed" the const char* version.)
Ideally I should be able to write the following (since boost::format overloads operator<<)
Code:
int main()
{
int x;
int low = 10;
int high = 20;
Input::Read(
x,
"Enter an integer: ",
"That is not a valid integer. Try again.",
boost::format("The integer must be bigger than %d and lower than %d.") % low % high,
ValidateRange(low, high)
);
std::cout << "You entered: " << x << std::endl;
}