C++中的<?= 运算符是什么意思?

8
我在代码中看到了 = 的用法:http://community.topcoder.com/stat?c=problem_solution&rm=151152&rd=5854&pm=2923&cr=310333 我尝试了一下不包含它们的编译,以测试它是否是标准的,但却失败了。之后我添加了这些内容,但仍然出现同样的错误:

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;
}

以下是链接中代码中对它的使用方式:
x <?= h[i][j];  // x = (h[i][j] < x) ? h[i][j] : x;

我该如何让这个工作起来?

可能是什么意思?的重复问题。 - David G
请选择最佳答案。 - David G
2个回答

9

这些是GCC扩展运算符。 a <?= b 的含义与 a = min(a, b) 相同(>?= 是“max”运算符),但它仅计算其左侧表达式一次。当 a 是变量时,这并不重要,但当 a 表示一个表达式时,特别是当表达式具有副作用时,可能会有所区别。例如,在以下情况下:

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

dest++中的++只会被评估一次。

这两个运算符现在都已经弃用


1
我不确定是否完全相同,因为 <?= 运算符似乎只会对 a 求值一次(例如考虑 f() <?= g())。 - Kerrek SB

3

这是GCC的扩展语法。x >?= y相当于:

x = max(x, y);

虽然我已经有一段时间没有看到它被使用了。


这句话应该是 x = max(x, y),对吧? - Mats Petersson
1
@tacp:它们在这里被提到(http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Deprecated-Features.html#Deprecated-Features),但只是说它们已经被移除了。 - Mike Seymour

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接