负数字字符串(例如“-10”)到无符号短整型 [英] Negative numeric string (e.g. "-10") to unsigned short

查看:232
本文介绍了负数字字符串(例如“-10”)到无符号短整型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using std::cerr;
using std::cout;
using std::stringstream;
using std::string;
using std::for_each;

void convert(const string& a_value)
{
    unsigned short i;
    if (stringstream(a_value) >> i)
        cout << a_value << " converted to " << i << ".\n";
    else
        cerr << a_value << " failed to convert.\n";
}

int main()
{
    string inputs[] = { "abc", "10", "999999999999999999999", "-10", "0" };
    for_each(inputs, inputs + (sizeof(inputs)/sizeof(inputs[0])), convert);
    return 0;
}


$ b

Output from Visual Studio Compiler (v7, v8, v9, v10):


abc failed to convert.
10 converted to 10.
999999999999999999999 failed to convert.
-10 converted to 65526.
0 converted to 0.

从g ++输出(v4.1.2, a href =http://ideone.com/T2sMF3> v4.3.4 ):

Output from g++ (v4.1.2, v4.3.4):


abc failed to convert.
10 converted to 10.
999999999999999999999 failed to convert.
-10 failed to convert.
0 converted to 0.

我希望 - 10无法转换为无符号short ,但它与VC编译器成功。这是在VC编译器中的

I expected the "-10" to fail to be converted to an unsigned short but it succeeds with the VC compilers. Is this a:


  • 错误吗?

  • 有不正确的期望?

  • 实施定义的行为?

推荐答案

答案取决于您使用的是什么版本的C ++。 C ++ 03和
之前需要输入符合 sscanf 所做的(使用这里
%hi 输入说明符), sscanf 将整数值读入
a(signed)short,然后将结果
赋值给您的 unsigned short (隐式转换)。 C ++ 11
需要等价于调用 strtoull ,这不允许
- 符号,并且在溢出的情况下需要错误(在 sscanf 中未定义
行为,因此C ++ 03)。

The answer depends on what version of C++ you are using. C++03 and earlier required the input to conform to what sscanf does (using here the "%hi" input specifier), and sscanf reads an integral value into a (signed) short, with no overflow detection; the results are then assigned (with implicit conversion) to your unsigned short. C++11 requires the equivalent of calling strtoull, which doesn't allow the - sign, and requires an error in case of overflow (which is undefined behavior in sscanf, and thus C++03).

在实践中,C ++ 03的所有合理实现都检查
overlow,在这种情况下的未定义行为对应于
,现在需要。另一方面,他们需要
接受减号,现在(逻辑上)被禁止。

In practice, all reasonable implementations of C++03 did check for overlow, and the "undefined behavior" in such cases corresponded to that which is now required. On the other hand, they were required to accept the minus sign, which is now (logically) forbidden.

EDIT(更正):
在重新读取 strtoull 的要求时,接受减号。所以像看起来那样愚蠢,标准确实需要输入无符号整数类型来接受减号。 (还要注意, strtoull 的行为取决于全局C语言环境,它可能接受额外的可能性。)

EDIT (correction): On rereading the requirements of strtoull, I find that it does require accepting the minus sign. So as stupid as it seems, the standard does require input to an unsigned integral type to accept the minus sign. (Note too that the behavior of strtoull depends on the global C locale, which may accept additional posibilities.)

编辑(进一步澄清):
as ectamur指出,这应该是一个错误(在C ++ 11),因为(unsigned long long)(-10)将太大,无法在无符号短中表示。另一方面,它仍然是未定义的行为在前C ++ 03(这也许是VC ++是符合的---所以无论他们做的是正确的)。

EDIT (further clarification): As ectamur points out, this should be an error (in C++11), because (unsigned long long)( -10 ) will be too large to be represented in an unsigned short. On the other hand, it is still undefined behavior in pre-C++03 (which is perhaps what VC++ is conforming to---so whatever they do is "correct").

这篇关于负数字字符串(例如“-10”)到无符号短整型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆