这很像这个问题Why must a short be converted to an int before arithmetic operations in C and C++? 然而,有一个子问题,为什么编译器在一种情况下诊断为警告,而在另一种情况下诊断为错误,表达式完全相同。
我真的很喜欢在 auto var =... 中使用 auto“类型”,但是 MSVC 2015 CTP 从我的代码中给出了一个错误。
问题是我正在auto-ing 类型short 的表达式,但有时它会被提升为int。
这是一个 MCVE:
struct MY_COORD { short X; short Y; };
using t_crd = MY_COORD;
void call_test ( t_crd x ) {}
int main()
{
t_crd crd { 10 ,20 };
auto x5 = crd.X - crd.Y;
auto y5 = crd.Y - crd.X;
t_crd crd5 { x5 ,y5 }; // (1)
call_test( t_crd{ x5 ,y5 } ); // (2)
}
第(1)行和第(2)行的消息分别是:
warning C4838: conversion from 'int' to 'short' requires a narrowing conversion
error C2397: conversion from 'int' to 'short' requires a narrowing conversion
我认为这段代码没问题,但它不是根据 MSVC 2015 CTP 编译器(但 Intellisense 没有指出)。 是否有一些我错过的模糊小规则使 MSVC 正确?
如果是这样,为什么相同的表达式在一种情况下是警告,而在另一种情况下是错误?
我想像这样在循环变量的初始化中使用它:
MY_COORD pos = ResultFromFunction();
auto rows = pos.Y;
for ( auto i = (rows - rows); i < rows; ++i )
{
coord c{ 0 ,i };
...
}
最佳答案
算术运算符总是在执行操作之前将小于 int 的整数类型提升为 int。这是 C++11 标准的 [expr]/10 中强制要求的:
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions.
那里适用于您的案例的子条款指定 rows - rows 中的操作数将进行积分提升。特别是,根据 [conv.prom]/1
A prvalue of an integer type other than
bool,char16_t,char32_t, orwchar_twhose integer conversion rank is less than the rank ofintcan be converted to a prvalue of typeintifintcan represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of typeunsigned int.
由于 short 的等级保证小于 int 的等级,因此 rows - rows 的操作数都被提升为int,给表达式 rows - rows int 类型。
关于窄化转换,标准规定(在[dcl.init.aggr]),在聚合初始化中,如果表达式需要窄化转换,程序是病式的。编译器可以随意诊断它。 IIRC,在像 t_crd crd5 { x5 ,y5 }; 这样的简单情况下,clang 3.5 发出错误,而 g++ 4.9.2 在使用默认警告设置时发出警告。无论哪种方式。 MSVC 的行为即使有点奇怪,也是符合标准的。
关于c++ - 自动类型推导没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29068333/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)