Ternary operator did not work in visual studio ARM64 MSVC release version.

Cheng Yang 0 Reputation points
2025-03-21T07:43:36.65+00:00

Env:

PC: Qualcomm X Elite(ARM64)

Visual Studio 2022 PRO

Platform Toolset: MSVC v143(14.43.34808)

Windows SDK version: 10.0.26100.0(can be others)

below is the sample code:

#include <iostream>


int main()
{


    typedef unsigned long long int ulonglong; /* ulong or unsigned long long */
    typedef long long int	longlong;
    
    long long int result;
    std::cin >> result;
    bool unsigned_flag;
    std::cin >> unsigned_flag;
    double d;

	d = unsigned_flag ? (double)((ulonglong)result) : (double)result;
    std::cout << d << "\n"; 
    return 0;
}

I found that ternary operator can not be operated properly in ARM64 release compilation. In the sample, if result variable is -11, then whether unsigned_flag variable is true or false, the d variable is 1.84467e+19 (0xFFFFFFFFFFFFFFF5), which means d is assigned by (double)((ulonglong)result).

Also, I checked the generated assembly code as follow:

User's image

sp+8 is the address of result variable, after ldr executed in address 0x00007FF6BB161108, result variable is directly converted to d0 using ucvtf, which assumes X8 is a unsigned variable and do not care the value of unsigned_flag variable.

But if I run debug version, the generated assembly code would check unsigned_flag variable, and use scvtf and ucvtf instructions correctly.

ps: If I use clang-cl platform toolset, the result is correct.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,916 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,291 Reputation points
    2025-03-21T09:46:24.04+00:00

    @Cheng Yang, Welcome to Microsoft Q&A, based on my test, I find that I always can get fasle for unsigned_flag either I input true or false. The problem is that the code std::cin >> unsigned_flag; cannot accept the correct value from console.

    I recommend that you could use the following code to get accept value for unsigned_flag.

    std::cin >> std::boolalpha >> unsigned_flag;
    
    
    

    Please feel free to let me know if you still meet the problem.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.