5

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.)

Community
  • 1
  • 1
Jim Buck
  • 20,482
  • 11
  • 57
  • 74

1 Answers1

2

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Well, I am dealing with legacy code that had exactly `x = x++;`. It took a while to track down the symptom I was seeing to this particular line of code. Having a warning would have absolutely been of practical use. – Jim Buck Jul 07 '12 at 03:12
  • Actually, it seems gcc does detect this (via -Wsequence-point) but not clang (which is what I'm using on OSX) - http://llvm.org/bugs/show_bug.cgi?id=4579 – Jim Buck Jul 07 '12 at 03:20
  • @Jim Buck: Yes, GCC is known to detect this. I'm not aware of any other compiler that does. – AnT stands with Russia Jul 07 '12 at 16:01
  • Yeah, that's a bummer. The best I could do to try to find this very specific case is a "file search" using a regexp. – Jim Buck Jul 07 '12 at 18:18