Possible minor bug in Visual C++ 2010/2012 regarding conditional operator - by Xavier Roche

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 754807 Comments
Status Closed Workarounds
Type Bug Repros 3
Opened 7/20/2012 7:53:22 AM
Access Restriction Public

Description

The following example:

const static volatile double zero = 0.0;

#define EXP(E) printf("%s == %u\n", #E, E)

int main(int argc, char **argv) {
  EXP(zero ? 1 : 0);
  EXP(-zero ? 1 : 0);
  EXP(0.0 ? 1 : 0);
  EXP(-0.0 ? 1 : 0);

  return EXIT_SUCCESS;
}

Should print the following result:

zero ? 1 : 0 == 0
-zero ? 1 : 0 == 0
0.0 ? 1 : 0 == 0
-0.0 ? 1 : 0 == 0

However, the last two lines shows "1" using Visual C++ 2010 (cl.exe 16.00.30319.01 for x64 or x86)

The same behavior is seen in the latest release, too, according to David Lowndes (*)

It was said (comp.lang.c) that according to the C standard, it should be 0 (3.3.15, "Conditional operator")

(*) See http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/8caf1a5a-d421-4a92-b924-468327afaec3
Sign in to post a comment.
Posted by Microsoft on 9/6/2012 at 3:59 PM
Hi Xavier:
    A fix for this issue has been checked into the compiler sources. The fix should show up in the next major release of Visual C++.
    To workaround the issue in VS2012 or earlier version, you can change the condition to explicit comparision. For example,

    (zero != 0.0) ? 1 : 0

Xiang Fan
Visual C++ Team
Posted by Microsoft on 7/22/2012 at 11:06 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 7/20/2012 at 8:50 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)