Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 914743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:46:06+00:00 2026-05-15T17:46:06+00:00

So I ran into something interesting that I didn’t realize about the ternary operator

  • 0

So I ran into something interesting that I didn’t realize about the ternary operator (at least in Visual C++ 98-2010). As pointed out in http://msdn.microsoft.com/en-us/library/e4213hs1(VS.71).aspx if both the expression and conditional-expression are l-values the result is an l-value.

Of course normally in c/c++ you’d write something like:

int value = (x == 1) ? 1 : 0;

and never even care about the r-value/l-value involvment, and in this case neither 1 nor 0 are convertible to l-values.

However, take something like:

int value = (x == 1) ? y : z;

both y and z are l-values and they, or more precisely, one of them is the actual result of the ternary operator (not its stored value) which isn’t necessarily obvious (at least I had never thought about it at any length).

But, what that leads to is the ability to write the following

(x == 1 ? y : z) = 99;

Which assigns 99 to y if x == 1 or 99 to z if x != 1

I’ve never seen that described anywhere and in all the discussions I’ve read about the use (or, usually, whether to use) the ternary operator.

Of course it only works if both the expression and conditional-expression are l-values something like

(x == 1 ? 0 : z) = 99;

fails to compile because 0 is an r-value as happily pointed out by the compiler.

And this only works if you include the parenthesis

x == 1 ? y : z = 99;

is something entirely different which assigns 99 to z only if (x != 1) and the beautiful part is that both sides are still l-values so there is the serious rat-hole of what things like (x == 1 ? y : z = 99) = 100 do (it assigns 100 to y or z depending on the truth of x == 1, stomping on the z = 99 assignment if x==1 is false)

So, this leads me to my questions:

A) Is this part of the actual c++ standard (which seems like it would be) and not just a Microsoft thing — I’ve looked but have failed, so far, to find this info.

B) If this is widely realized and I’ve been living under a rock? I’ve never seen it used in any code that I can recall, and never seen it mentioned when the ternary operator is discussed.

C) Do I need to get out more often?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:46:07+00:00Added an answer on May 15, 2026 at 5:46 pm

    A) Yes, this is part of the standard.

    B) It’s not widely realized, though it may be here on SO. There’s a reason it was voted the #1 hidden feature of C++: Hidden Features of C++?.

    C) No comment. 🙂

    Personally, I recommend steering clear of using this feature. It is a lot less intuitive than using if/else statements, and clearly not everyone knows about it.

    Going against my own warning, I actually tried using this once on a personal project, and I got burned by missing the parentheses and wasting 30 minutes trying to find the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.