gpt4 book ai didi

c++ - stringstream 读取失败是不确定的吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:42:00 29 4
gpt4 key购买 nike

我有一些代码在不同的编译器上表现不同:

std::string strVal = "11";
std::stringstream stream( strVal );
auto value = false;

if( !( stream >> std::noboolalpha >> value ) )
{
// report error - this path is taken on both compilers
}

// "value" is "false" here on in VS 2017, but "true" on GCC 4.6

行为上的差异是预期的,还是 GCC 中的错误?

根据 cppprefence :

If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set.

但是,value 在 GCC 4.6 上被修改为 true

最佳答案

MSVC 错误。 [facet.num.get.virtuals]/6:

Effects: If (str.flags()&ios_­base​::​boolalpha) == 0 then input proceeds as it would for a long except that if a value is being stored into val, the value is determined according to the following: If the value to be stored is 0 then false is stored. If the value is 1 then true is stored. Otherwise true is stored and ios_­base​::​failbit is assigned to err.

关于c++ - stringstream 读取失败是不确定的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50009097/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com