0
从字符串提取数据到标量(char,short,int ...)时,我怎么能容易地知道我想获取的值是否超出类型限制?C++在将字符串提取到标量时处理上溢/下溢
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;
}