负数lexical_cast在不同的计算机上表现不同

lexical_cast with negative number behaves differently on different machines

本文关键字:计算机 lexical cast 负数      更新时间:2023-10-16

我在两个不同的设备上有相同版本的 boost,但行为不同

lexical_cast<uint>("-1")

文档指出它应该给我INT_MAX(2 的补码翻转),但在一台机器上我得到一个异常抛出,而在另一台机器上我得到INT_MAX。

显然,

如果您查看 boost 的代码lexical_cast输入是使用这样的代码加载和操作的 interpreter << arg; interpreter >> result>> 运算符

this->setg(start, start, finish);             
std::basic_istream<CharT> stream(static_cast<Base*>(this));
stream.unsetf(std::ios::skipws);

它使用std::basic_istream因此不同版本的libstdc++将导致lexical_cast在不同的机器上表现不同。

相关文章: