8

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,

Betamoo
  • 14,964
  • 25
  • 75
  • 109

7 Answers7

11

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.

Mike Caron
  • 14,351
  • 4
  • 49
  • 77
8

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.

  • 2
    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 Jul 27 '10 at 19:59
  • 1
    @Betamoo: Choose this one instead. All the information is here. At least now it is :) – Magnus Hoff Jul 27 '10 at 20:01
  • 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 :) –  Jul 27 '10 at 20:08
4

How's that:

(x<value) || (x=value)
Nordic Mainframe
  • 28,058
  • 10
  • 66
  • 83
1

Are you thinking of the ternary operator?

result = a > b ? x : y;
Donald Miner
  • 38,889
  • 8
  • 95
  • 118
  • Yes he does. Except that it isn't called trenary operator (any operator with three operands is trenary) but conditional operator. –  Jul 27 '10 at 19:51
  • 1
    Since there's only one ternary operator in C & C++ (for now?) I think it will be pretty clear to everybody what you're talking about... – Eugen Constantin Dinca Jul 27 '10 at 19:55
  • 5
    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. – corsiKa Jul 27 '10 at 19:56
0
x = x < value ? 0 : 1;

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

thkala
  • 84,049
  • 23
  • 157
  • 201
Dan
  • 1
0

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;
thkala
  • 84,049
  • 23
  • 157
  • 201
Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
0

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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    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. – supercat Jul 27 '10 at 20:28