<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Different type conversion in ternary operator between clang and gcc"
   href="http://llvm.org/bugs/show_bug.cgi?id=15199">15199</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Different type conversion in ternary operator between clang and gcc
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>klimek@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following snippet:
#include <cstdint>
#include <stdio.h>

const  int64_t kint64max  = ((int64_t)0x7FFFFFFFFFFFFFFFLL);

int main() {
  double d = (double)kint64max;
  int64_t v = d < kint64max ? d : kint64max;
  printf("%.20f -> %ld\n", d, v);
  return 0;
}

/tmp$ ~/build/llvm-opt/bin/clang++ -std=c++11 -o tt t.cc  && ./tt
9223372036854775808.00000000000000000000 -> -9223372036854775808
/tmp$ g++ -std=c++0x -o tt t.cc && ./tt
9223372036854775808.00000000000000000000 -> 9223372036854775807

Changing this to:
  int64_t v = d < kint64max ? (int64_t)d : kint64max;

/tmp$ ~/build/llvm-opt/bin/clang++ -std=c++11 -o tt t.cc  && ./tt
9223372036854775808.00000000000000000000 -> 9223372036854775807

The question is whether clang or gcc is correct, or there is something
different I'm not understanding going on here. Chandler suggested I file a bug
and let the language lawyers take a shot :)

The AST for the original code from clang looks like this:
     (ImplicitCastExpr 0x31628d0 <col:7, col:27> 'int64_t':'long'
<FloatingToIntegral>
        (ConditionalOperator 0x31628a0 <col:7, col:27> 'double'
          (BinaryOperator 0x31627e0 <col:7, col:11> '_Bool' '<'
            (ImplicitCastExpr 0x3162798 <col:7> 'double' <LValueToRValue>
              (DeclRefExpr 0x3162748 <col:7> 'double' lvalue Var 0x31625e0 'd'
'double'))
            (ImplicitCastExpr 0x31627c8 <col:11> 'double' <IntegralToFloating>
              (ImplicitCastExpr 0x31627b0 <col:11> 'int64_t':'long'
<LValueToRValue>
                (DeclRefExpr 0x3162770 <col:11> 'const int64_t':'const long'
lvalue Var 0x31623d0 'kint64max' 'const int64_t':'const long'))))
          (ImplicitCastExpr 0x3162858 <col:23> 'double' <LValueToRValue>
            (DeclRefExpr 0x3162808 <col:23> 'double' lvalue Var 0x31625e0 'd'
'double'))
          (ImplicitCastExpr 0x3162888 <col:27> 'double' <IntegralToFloating>
            (ImplicitCastExpr 0x3162870 <col:27> 'int64_t':'long'
<LValueToRValue>
              (DeclRefExpr 0x3162830 <col:27> 'const int64_t':'const long'
lvalue Var 0x31623d0 'kint64max' 'const int64_t':'const long')))))"))</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>