当从字符串提取数据到标量(char、short、int ...)时,我怎么能很容易地知道我想要获取的值是否超过了类型限制?
unsigned char function(void)
{
std::string str = "259";
std::ostringstream os(str);
unsigned char scalar; // could also be short, int, float or double
if (str > /* limit of char */)
{
/* throw exception */
}
os >> scalar;
return scalar;
}