在 Visual Studio 2015 中的符号扩展操作数上使用的按位或运算符 [英] Bitwise-or operator used on a sign-extended operand in Visual Studio 2015

查看:32
本文介绍了在 Visual Studio 2015 中的符号扩展操作数上使用的按位或运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚尝试安装 Visual Studio 2015,在尝试编译旧项目时,收到警告

I just tried installing Visual Studio 2015, and when trying to compile an old project, I got the warning

CS0675 用于符号扩展操作数的按位或运算符;考虑首先转换为较小的无符号类型

CS0675 Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first

对于一段在 Visual Studio 2013 中编译时没有给出相同警告的代码.我发现重现所需的只是这段非常简单的代码:

for a piece of code that does not give the same warning when compiling in Visual Studio 2013. I found out that all it takes to reproduce is this very simple code:

short a = 0;
int b = 0;

a |= (short)b;

现在,我已经阅读了 这个问题,我已阅读Eric Lippert 的博客文章 关于这个问题,我很快就阅读了 符号扩展,但我的理解是,当您从包含较少位数的有符号数类型转换为具有较多位数的有符号数类型时,会发生符号扩展,例如shortint 例如.

Now, I have read this SO question, I have read Eric Lippert's blog post on this issue, and I quickly read up on sign extension, but my understanding is that sign extension happens when you cast from a signed number type consisting of a smaller number of bits to one with a larger number of bits, such as short to int for example.

但是由于我从 int 转换为 short,如果我没记错的话,应该不会发生符号扩展.事实上,这不会在 Visual Studio 的早期版本中发出警告,这让我相信这一定是 Visual Studio 2015 编译器 (Roslyn) 中的错误.我是否误解了符号扩展和/或编译器在这里的工作方式,或者这很可能是编译器错误?

But since I'm casting from an int to a short, no sign extension should happen if I'm not mistaken. The fact that this does not issue a warning in earlier versions of Visual Studio, it leads me to believe that this must be a bug in the Visual Studio 2015 compiler (Roslyn). Am I misunderstanding how sign extension and/or the compiler works here, or is this most likely a compiler bug?

Jon Skeet 指出实际上确实发生了符号扩展,因为 | 运算符没有为 short 定义,所以有一个隐式转换为 int 在结果再次转换回 short 之前.但是,编译器不应该发出此警告,因为强制转换是无害的.正如已接受的答案中所指出的,Roslyn 编译器中存在一个错误.

Jon Skeet pointed out that there actually is indeed a sign extension happening since the | operator isn't defined for short and so there's an implicit cast to int before the result is cast back to short again. However, the compiler shouldn't have issued this warning since the cast is harmless. There was a bug in the Roslyn compiler as pointed out in the accepted answer.

推荐答案

这只是一个错误.检测和报告此错误的代码是在 VS2015 开发中很晚才添加的(参见 https://github.com/dotnet/roslyn/issues/909https://github.com/dotnet/roslyn/pull/2416),与 VS2013 相比,它检测到的案例过多.现在有一个错误报告(https://github.com/dotnet/roslyn/issues/4027) 修复此问题.

This is just a bug. The code to detect and report this error was added very late in the VS2015 development (see https://github.com/dotnet/roslyn/issues/909 and https://github.com/dotnet/roslyn/pull/2416) and it detects too many cases compared to VS2013. There is now a bug report (https://github.com/dotnet/roslyn/issues/4027) to repair this.

这篇关于在 Visual Studio 2015 中的符号扩展操作数上使用的按位或运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆