当块返回值时,它叫什么? [英] What is it called when a block returns a value?

查看:149
本文介绍了当块返回值时,它叫什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到这个代码,这看起来不合法我(但gcc编译它)。我不太在意这个结构,因为它想要一个名字:

  #define MAX(a,b)\ 
({\
typeof(a)_a =(a); \
typeof(b)_b =(b); \
(_a& ?(_a):(_b); \
})

显然,语句的值将作为由命名空间限定的表达式的值返回。



编辑:感谢您的答案。原来这是一个扩展到纯C的语句表达式。

解决方案

不是命名空间是宏返回最大值两个值。

\ 在语句结尾处用于附加多个语句并创建多行宏。 p>

代码不是标准C ++,而是在gcc中编译,因为它支持 gcc编译器扩展



良好阅读:



语句表达式

复合语句是由大括号括起来的一系列语句。在GNU C中,括号中的复合语句可能会在所谓的语句表达式中显示为表达式。

  .--------------。 
V |
>> - ( - {---- statement - ; - + - } - )--------------------- -----------><

语句表达式的值是整个构造中出现的最后一个简单表达式的值。如果最后一个语句不是表达式,那么结构的类型为void,并且没有值。



注意:这段摘录取自IBM XL C / C ++ v7。 0文档。


I came across this code recently, which doesn't look legal to me (but gcc compiles it). I don't so much mind the construction as want a name for it:

#define MAX(a,b) \
({ \
    typeof(a) _a = (a); \
    typeof(b) _b = (b); \
    (_a > _b) ? (_a) : (_b); \
})

Apparently, the last statement's value is being returned as the "value" of the expression bounded by the namespace.

Edit: Thanks for the answers guys. Turns out this is an extension to plain C called Statement Expressions.

解决方案

It is not a namespace, it is a macro which returns maximum of two values.
\ at the end of the statements is use to append multiple statements and create a multi-line macro.

The code is not standard C++ but it compiles in gcc because it is supported as an gcc compiler extension.

Good Read:

Statement Expressions:
A compound statement is a sequence of statements enclosed by braces. In GNU C, a compound statement inside parentheses may appear as an expression in what is called a Statement expression.

         .--------------.
         V              |
>>-(--{----statement--;-+--}--)--------------------------------><

The value of a statement expression is the value of the last simple expression to appear in the entire construct. If the last statement is not an expression, then the construct is of type void and has no value.

Note: This excerpt is taken from IBM XL C/C++ v7.0 documentation.

这篇关于当块返回值时,它叫什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆