I have a situation where I am grabbing command line arguments and using boost::lexical_cast<unsigned long>(my_param)
. I was hoping that negative values of my_param would cause lexical_cast to throw, but instead it happily converts them, with -1 becoming 18446744073709551615
. Which seems absurd, as the max value for an unsigned long is 2^32-1, it looks much more like an unsigned long long.
So I am looking for either a smarter way to cast the char * input to unsigned long, or a way to verify that I have not accepted a negative value in its disguise as a large unsigned long long.
if (my_param && *my_param != '-')
?