3

Here is a question that asks whether VC++ does consider it undefined, and here: Undefined behavior is closer than you think is a post that clearly shows that it is in fact undefined and the compiler/optimizer may mess up code in VS2015 no less.

What I haven't found so far is whether VC++ has any switch like -fwrapv of GCC to make the compiler/optimizer have signed overflow well defined. Is there any such option?

Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • Even if there is such an option for VS, enabling it technically makes your language not C++ any more. The C++ standard explicitly (if in a roundabout way) says that signed integer overflow is undefined, if you want your code to be portable, even to other versions of the same compiler, you can't use extensions like that. – Some programmer dude Nov 02 '15 at 14:55
  • 1
    @JoachimPileborg - don't care whether it's "pure". If it can eliminate a source of bugs for a legacy codebase, I'm not shy to use it. – Martin Ba Nov 02 '15 at 15:19
  • @JoachimPileborg - and with `-fwrapv` *of course* you can port to all versions of the compiler that also have this option and it will be perfectly defined, because the compiler docs say so. – Martin Ba Nov 02 '15 at 15:21
  • 4
    MSVC seems to have a undocumented compiler flag `–d2UndefIntOverflow–`. https://devblogs.microsoft.com/cppblog/new-code-optimizer/ – fujii Apr 15 '19 at 05:14
  • @fujii - thanks. Very interesting link. This is only from VS2015 Update 3 upwards as far as I can tell. I gotta try that switch though :-) – Martin Ba Apr 16 '19 at 14:32

1 Answers1

4

If you check here at Microsoft's Compiler Options Listed by Category there doesn't seem to be any such option.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203