Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get VS or Xcode warning with something like "x = x++"?

In the spirit of undefined behavior associated with sequence points such as “x = ++x” is it really undefined?, how does one get the compiler to complain about such code?

Specifically, I am using Visual Studio 2010 and Xcode 4.3.1, the latter for an OSX app, and neither warned me about this. I even cranked up the warnings on VS2010 to "all", and it happily compiled this. (For the record, VS2010's version added 1 to the variable where Xcode's version kept the variable unchanged.)

like image 724
Jim Buck Avatar asked Nov 04 '22 20:11

Jim Buck


1 Answers

AFAIK, Visual Studio does not detect such situations. And I'm personally not convinced that there's much point in doing that. I'd say that a warning for a fairly obvious case like that would have mostly educational value, but not much practical value. In general case the issue in question takes place in situations like *p = ++*q when p and q happen to point to the same object. Needless to say, they are not detectable by the compiler.

like image 129
AnT Avatar answered Nov 12 '22 13:11

AnT