例如,
#include <iostream>
int main() {
unsigned n{};
std::cin >> n;
std::cout << n << ' ' << (bool)std::cin << std::endl;
}
输入-1时,clang 6.0.0输出 0 0 而 gcc 7.2.0输出 4294967295 1。我想知道谁是正确的。或者也许两者都是正确的标准没有指定这一点?如果失败,我的意思是 (bool)std::cin 被评估为假。 clang 6.0.0 也无法输入 -0。
从 Clang 9.0.0 和 GCC 9.2.0 开始,在 Clang 的情况下使用 libstdc++ 或 libc++ 的两个编译器都同意上述程序的结果,与 C++ 版本无关 (>= C++11 ) 使用并打印
4294967295 1
即他们将值设置为 ULLONG_MAX 并且没有在流上设置故障位。
最佳答案
我认为在 C++171 中两者都是错误的,预期的输出应该是:
4294967295 0
虽然返回的值对于两个编译器的最新版本都是正确的,但我认为 ios_base::failbit应该设置,但我也认为标准中要转换的字段的概念存在混淆,这可能会解释当前的行为。
标准说——[facet.num.get.virtuals#3.3] :
The sequence of chars accumulated in stage 2 (the field) is converted to a numeric value by the rules of one of the functions declared in the header
<cstdlib>:
For a signed integer value, the function
strtoll.For an unsigned integer value, the function
strtoull.For a floating-point value, the function
strtold.
所以我们退回到 std::strtoull ,必须返回2 ULLONG_MAX 并且未设置 errno在这种情况下(这是两个编译器所做的)。
但在同一 block 中(强调是我的):
The numeric value to be stored can be one of:
zero, if the conversion function does not convert the entire field.
the most positive (or negative) representable value, if the field to be converted to a signed integer type represents a value too large positive (or negative) to be represented in
val.the most positive representable value, if the field to be converted to an unsigned integer type represents a value that cannot be represented in
val.the converted value, otherwise.
The resultant numeric value is stored in
val. If the conversion function does not convert the entire field, or if the field represents a value outside the range of representable values,ios_base::failbitis assigned toerr.
请注意,所有这些都是关于“要转换的字段”,而不是 std::strtoull 返回的实际值。 .这里的字段实际上是字符'-', '1'的加宽序列.
由于该字段表示的值 (-1) 不能用 unsigned 表示,返回值应为 UINT_MAX并且故障位应设置为 std::cin .
1 clang实际上在 C++17 之前是正确的,因为上面引用中的第三个项目符号是:
- the most negative representable value or zero for an unsigned integer type, if the field represents a value too large negative to be represented in
val.ios_base::failbitis assigned toerr.
2 std::strtoull 返回 ULLONG_MAX因为(感谢@NathanOliver)- C/7.22.1.4.5:
If the subject sequence has the expected form and the value of base is zero, the sequence of characters starting with the first digit is interpreted as an integer constant according to the rules of 6.4.4.1. [...] If the subject sequence begins with a minus sign, the value resulting from the conversion is negated (in the return type).
关于c++ - 是否应该通过 std::cin 将负数读入 unsigned 失败(gcc,clang 不同意)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921271/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下