8

I saw <?= and >?= used in a code: http://community.topcoder.com/stat?c=problem_solution&rm=151152&rd=5854&pm=2923&cr=310333

I tried to compile without the includes to test if it's standard, but it didn't work. I then added the includes, but it still gives the same error:

question-mark.cpp:15:5: error: expected primary-expression before ‘?’ token question-mark.cpp:15:6: error: expected primary-expression before ‘=’ token question-mark.cpp:15:9: error: expected ‘:’ before ‘;’ token question-mark.cpp:15:9: error: expected primary-expression before ‘;’ token

#include <stdio.h>
#include <algorithm> 
#include <iostream> 
#include <sstream> 
#include <string> 
#include <vector> 

using namespace std;

int main()
{

    int x = 3;
    int y = 2;
    x >?= y;
    printf("x = %d\n", x);

    return 0;
}

Here's how it is used in the code from the link:

x <?= h[i][j];  // x = (h[i][j] < x) ? h[i][j] : x;

How can I make this work?

Leandro
  • 376
  • 5
  • 16

2 Answers2

9

These are GCC extensions operators. a <?= b has the same meaning as a = min(a, b) (>?= is the "max" operator), but it evaluates its left-hand side expression only once. This is not important when a is a variable, but it may make a difference when a represents an expression, especially when the expression has a side effect. For example, in

*dest++ <?= *src++;

the ++ in dest++ would be evaluated only once.

Both operators have now been deprecated.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    I'm not sure it's exactly the same, since the `=` operator only seems to evaluate `a` once (e.g. consider `f() = g()`). – Kerrek SB Apr 21 '13 at 00:44
3

It's a GCC extension. x >?= y is equivalent to:

x = max(x, y);

I haven't seen it used in a while though.

David G
  • 94,763
  • 41
  • 167
  • 253