VS2012 unsigned negate

半城伤御伤魂 提交于 2019-12-25 17:22:37

问题


In code that is supposed to be quite portable, in a many person, many environment project, I have to deal with a problem with code that negates unsigned int (and same problem in other places negating std::size_t).

The intent is to create the additive inverse of that value (modulo MAX_VAL+1 where MAX_VAL is the max value of the type) and I found other questions on this topic confirm that is the standard specified behavior.

VS2012, (with compile time options I cannot easily learn) calls that line an error. The developer, with access to that environment, changed -U to -1*U, which compiles in that environment. I have a low opinion of that change and I'm not happy with the work I would need to go through to portability test that change to code that was correct in the first place.

I expect there is some #pragma, which will tell VS2012 to allow -U to have its standards defined meaning. I expect that will be easier to find than exploring any consequences of -1*U (beyond the fact that I think -1*U is disgusting).

But I'm hoping to get some language lawyer and/or VS2012 guru opinions before trying any of that.

Edit, I should have linked the relevant previous answer: What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

I believe the original code is correct based on that answer. I want to know how to make VS2012 believe the code is correct.

The error message, is:

error C4146: unary minus operator applied to unsigned type, result still unsigned


回答1:


#pragma warning (disable : 4146) // VS2012 unary minus operator applied to unsigned type

I didn't try to dig up what ill advised option someone added somewhere in the solution hierarchy to convert that unsound warning into a nasty error. Locally in the source code, in the several places where an unsigned or a std::size_t is correctly negated, the #pragma is quite reasonable as a comment to make it clear to any human code reviewer or MS compiler that negating an unsigned really was intentional.

I tested, that #pragma really does convert it all the way down from error to silent.

As a matter of project style, the compiler version in the comment on a warning disable tells only which compiler/version triggered the decision to add the #pragma. It is not intended to give any hint at how broad or narrow (other compilers or versions) the issue might be.

If I were in control of all aspects of this, I might decide that the command line option turning this warning into an error is so ill advised that it would be better to fix the command line than the source code (and worth all the extra effort to find the place whatever stupid option was injected into the command line). But in fact, I have no influence over that command line. I can make the final decision on what to do in these source files to compensate for bad decisions regarding the command line. I can't influence whatever I might consider to be bad decisions regarding the command line.



来源:https://stackoverflow.com/questions/33618480/vs2012-unsigned-negate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!