- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我将我的编译器从 gcc-4.4 升级到 gcc-4.8,一个项目因以下(错误的)假设而惨遭失败:
#include <sstream>
#include <assert.h>
int main()
{
using namespace std;
istringstream iScan;
int num;
//iScan.unsetf(std::ios::skipws);
iScan.str("5678");
iScan >> num;
assert(iScan.tellg() == istringstream::pos_type(4));
assert(!iScan.fail());
assert(!iScan.good());
assert(iScan.eof());
assert(num == 5678);
assert(false && "We passed the above assertions.");
return 0;
}
在 gcc-4.4 上,相关断言通过。在 gcc-4.8 上,tellg() 返回 -1,而 fail() 返回 !false,显然是因为它命中了 eof。
我的目标是 Qt 5.1 (gcc-4.8) 附带的 MinGW 32 位。
问题:
此外,各种在线编译器给出不同的行为。这是他们的库的功能吗?
其他类似但不重复的问题:
这些假设贯穿其中的代码主体非常庞大。
更新:这是答案的关键部分,应该适用于所有版本、所有编译器:
// istream::tellg() is unreliable at eof(): works w/gcc-4.4, doesn't w/gcc-4.8.
#include <sstream>
#include <assert.h>
using namespace std;
typedef istream::pos_type pos_type;
pos_type reliable_tellg(istream &iScan)
{
bool wasEOF = iScan.eof();
if (wasEOF)
iScan.clear(iScan.rdstate() & ~ios::eofbit); // so tellg() works.
pos_type r = iScan.tellg();
if (wasEOF)
iScan.clear(iScan.rdstate() | ios::eofbit); // restore it.
return r;
}
int main()
{
istringstream iScan;
int num, n2;
//iScan.unsetf(std::ios::skipws);
iScan.str("5678");
assert(!iScan.eof() && !iScan.fail()); // pre-conditions.
assert(reliable_tellg(iScan) == pos_type(0));
iScan >> num;
assert(!iScan.fail());
assert(reliable_tellg(iScan) == pos_type(4));
assert(iScan.eof());
assert(reliable_tellg(iScan) == pos_type(4)); // previous calls don't bungle it.
assert(num == 5678);
iScan >> n2; // at eof(), so this should fail.
assert(iScan.fail());
assert(reliable_tellg(iScan) == pos_type(-1)); // as expected on fail()
assert(iScan.eof());
assert(false && "We passed the above assertions.");
return 0;
}
最佳答案
您似乎期望的行为可能是错误的。两者都是 C++11和 C++03 以“Behaves as an未格式化的输入函数[...]”。一个“未格式化的输入function” 从构建一个 sentry
对象开始,并将失败,什么都不做并返回失败状态,如果sentry
对象转换为 false
。和 sentry
对象如果设置了 eofbit
,将转换为 false
。
标准对于是否阅读number 设置 eofbit
,但只是轻微的(与信息分布在几个不同的部分)。基本上,当输入一个数值时,流(实际上,num_get
方面)必须先读取一个字符,以便知道数字在哪里结束。在你的情况下,它将看到结束发生这种情况时的文件,因此将设置 eofbit
。所以你的第一个 assert
将因符合规范的实现而失败。
人们很容易认为这是标准中的缺陷,或者无意的。很容易想象一些实现做明智的事情(这似乎是你所期望的),也许是因为最初的实现者没有意识到标准中的全部含义(或不自觉地将其读作他们认为应该阅读)。我猜这是g++ 的情况,当他们意识到他们的行为是不合规,他们修复了它。
至于解决方法...我不确定真正的问题是什么,您正在尝试解决。但我认为如果你清除tellg
之前的错误位,它应该可以工作。 (的当然,那么 iScan.good()
将是 true
,而 iScan.eof()
错误
。但这真的很重要吗?)一定要检查一下在清除之前提取实际上成功了状态。
关于EOF : behavior change; work-around? 上的 C++ istream tellg()/fail(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18490576/
我想避免创建 std::thread 的开销,因此我要实现一个线程池。我正在为一个设计决策而苦苦挣扎: 工作队列中的工作是否应该能够将工作添加到工作队列中?如果是,如何? 问题出现了,因为我想让我添加
color 属性正常工作,但其他两个属性(font-size 和 text-shadow)不起作用。当链接被访问时,它的字体大小应该减小到 20 px 并且应用 text-shadow 属性,但它没有
我已经安装并配置了 supervisor。 ps -ax 显示 10 个进程,例如:php/home/vagrant/Sites/mysite/artisan queue:work --tries=1
我对 php artisan queue::work 命令感到不安。 我的命令不起作用,但我的作业已插入作业表但从未执行。 我正在为队列使用 mongodb 驱动程序。 我做错了什么,请给我建议。 最
为什么我可以找到很多关于“工作窃取”的信息而没有关于“工作耸肩”作为动态负载平衡策略的信息? 通过“工作耸肩”,我的意思是将多余的工作从繁忙的处理器转移到负载较低的邻居上,而不是让空闲的处理器从忙碌的
首先,我正在为 MySQL 使用 DATE_ADD 函数。当试图在 php 中使用 $sqlA 时,由于某种原因它说语法错误(主要是 WHERE 之后的区域)。为什么? $sqlA = "SELECT
a:hover { color: #237ca8 !important; font-weight: bold; } a:active { color: #cccccc !imp
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this q
我试图让只能使用 Tab 键的用户可以访问我的网站。我遇到的问题是,当我尝试使用 tab 键选择 float 的 div 时,不会触发 :focus in css;我不知道为什么它没有被触发。鼠标悬停
我在尝试将 2 个 div 并排放置时遇到了问题。 display: inline 它会删除我的边框并且不会将两个 div 放在同一行上。 请指教: .gig { outline: 1px s
这是 fiddle :http://jsfiddle.net/j9Gmx/ 我怎样才能得到最小高度:100%;上类? 最佳答案 它正在 工作,但由于 div 的父级(正文)没有高度,100% 基本上是
我正在使用 Flutter WebRTC 来创建 P2P 视频通话。 我遇到了一个与网络相关的问题:我已经完成了应用程序,但它只适用于移动数据。 将网络更改为WiFi时,它不起作用并且连接状态挂起Ch
我是 JavaScript 和 jQuery 的初学者。我的 css 和 JavaScript 代码位于 html 文件外部。这个问题已经有了答案,我尝试了所有代码,但滚动不起作用。我不知道我错过了什
我正在使用 Sprin AMQP 的rabbittemplate 通过 RabbitMQ 发送和接收消息。我能够发送和接收消息,但是,我想优先处理消息。 例如,如果我推送 1000 条消息,假设奇数消
我已经在 WorkManager 中加入了一个PeriodicWork,并希望每次完成时都获取它的 Worker 的输出数据,但以下代码似乎不起作用,因为 Log 消息没有出现在 Logcat 中:
我有一个名为 areaOne 的 AngularJS 指令。当我使用 template 时,会显示模板,但当我在 area1.js 中使用 templateUrl 时,不会呈现模板 HTML。 我在这
“:after”选择器在应用于带有 FF 和 IE 的输入时不起作用 input:after { content: "title"; } 而它正在处理 p、a 等。 这是一个错
下面是适用于 oracle 但不适用于 PostgreSQL 的 Sql 查询。 select count(*) from users where id>1 order by username; 我知
position?:fixed 在 chrome 浏览器上不工作,但在 firefox 中工作正常。 我有一个侧边栏可以停止滚动并固定在顶部。它在 firefox 中运行完美,但在 chrome 中,
我有一段代码无法在 Firefox 中运行。当按钮悬停时,.icon 图像不会改变。它在 Chrome 中完美运行。 button.add-to-cart-button .button-left .i
我是一名优秀的程序员,十分优秀!