201

7
+7  Q:

## C++ Conditional Operator

I once seen a -wired- operator in C++ which assigns value if greater than..
it was a combination of `?`, `<` and `=`

e.g. let x = value if value is greater than x

I do not mean `x=(x<value)x:value`

It was some sort of `x<?=value`

But I can not remember it exactly, and can not find it online... Can some one remind me of it?

Thanks,

+1  A:

Are you thinking of the ternary operator?

``````result = a > b ? x : y;
``````
Yes he does. Except that it isn't called trenary operator (any operator with three operands is trenary) but conditional operator.
No, Not that..!
Actually, it's pretty much universally called the ternary operator. C/C++/Java/Most every other language only have one operator that classifies as ternary, and this is it.
+10  A:

There is no operator that assigns variables based on their relative values.

However, there is the ?: operator:

``````x = value > x ? value : x;
``````

If you read it out loud from left to right, it makes sense.

Curious that this was downvoted for some reason...
A:

x = x < value ? 0 : 1;

This function sets x to 0 is x < value, 1 otherwise.

actually, in this case you can do `x = x >= value` to accomplish this, true evalutes to 1.
+7  A:

gcc has -- in version 3.3.6 at least! -- a gcc-specific language extension providing specialized operators for implementing min and max. Perhaps this is what you are thinking of?

Minimum and Maximum Operators in C++

I don't have gcc handy to test it with, but it might have an updating form, too.

Yes...that is so close to what I mean....
It is worth noting that these are deprecated: "The G++ minimum and maximum operators (`<?` and `>?`) and their compound forms (`<?=` and `>?=`) have been deprecated and will be removed in a future version. Code using these operators should be modified to use std::min and std::max instead." -- http://gcc.gnu.org/onlinedocs/gcc-4.0.1/gcc/Deprecated-Features.html
@Magnus Hoff: Put that as an answer and I will choose yours...
@Betamoo: Choose this one instead. All the information is here. At least now it is :)
Looks like I didn't pick quite the right search terms to pick the deprecation notice, apologies. (These HAVE been around for a while -- I remembered them from when I was using djgpp for DOS in the mid 90s... -- and haven't made it into the standard or any other compilers, so it was probably only a matter of time before somebody put them out of their misery :)
A:

it's a more convenient version of an if statement that is used for assignment int x = (some bool) ? trueval : falseval; this is roughly what it means, when the bool is evaluated it either gets the trueval or falseval depending on the outcome, it's easier than saying int x; if (someval) x = trueval; else x = falseval;

A:

I suspect what you're thinking of is a gcc extension1 that lets you leave out the middle operand to the conditional operator, so (for example):

``````a = b ? b : c;
``````

can be written as:

``````a = b ?: c;
``````

1 Despite the '2.95.3' in the URL, I'm not aware of a newer version of the linked page. If somebody is, please feel free to point it out (or edit it in).

That operator is how I've sometimes wished || worked. While there are times it's useful to have the "true" value forced to "1", there are other times it would be more useful to have the value of the first non-zero operand.
+4  A:

How's that:

``````(x<value) || (x=value)
``````
hah, nice :) +1
Somebody like's to use Perl :-)
*shudder* I hope to never see that in real code.