摘自GNU C中的语句表达式

6.1 Statements and Declarations in Expressions

A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.

Recall that a compound statement is a sequence of statements surrounded by braces; in this construct, parentheses go around the braces. For example:

     ({ int y = foo (); int z;
        if (y > 0) z = y;
        else z = - y;
        z; })

is a valid (though slightly more complex than necessary) expression for the absolute value of foo ().

The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct. (If you use some other kind of statement last within the braces, the construct has type void, and thus effectively no value.)

This feature is especially useful in making macro definitions “safe” (so that they evaluate each operand exactly once). For example, the “maximum” function is commonly defined as a macro in standard C as follows:

     #define max(a,b) ((a) > (b) ? (a) : (b))

But this definition computes either a or b twice, with bad results if the operand has side effects. In GNU C, if you know the type of the operands (here taken as int), you can define the macro safely as follows:

     #define maxint(a,b)        ({int _a = (a), _b = (b); _a > _b ? _a : _b; })

Embedded statements are not allowed in constant expressions, such as the value of an enumeration constant, the width of a bit-field, or the initial value of a static variable.

If you don‘t know the type of the operand, you can still do this, but you must use typeof or __auto_type (see Typeof).

In G++, the result value of a statement expression undergoes array and function pointer decay, and is returned by value to the enclosing expression. For instance, if A is a class, then

             A a;

             ({a;}).Foo ()

constructs a temporary A object to hold the result of the statement expression, and that is used to invoke Foo. Therefore the this pointer observed by Foo is not the address of a.

In a statement expression, any temporaries created within a statement are destroyed at that statement‘s end. This makes statement expressions inside macros slightly different from function calls. In the latter case temporaries introduced during argument evaluation are destroyed at the end of the statement that includes the function call. In the statement expression case they are destroyed during the statement expression. For instance,

     #define macro(a)  ({__typeof__(a) b = (a); b + 3; })
     template<typename T> T function(T a) { T b = a; return b + 3; }

     void foo ()
     {
       macro (X ());
       function (X ());
     }

has different places where temporaries are destroyed. For the macro case, the temporary X is destroyed just after the initialization of b. In the function case that temporary is destroyed when the function returns.

These considerations mean that it is probably a bad idea to use statement expressions of this form in header files that are designed to work with C++. (Note that some versions of the GNU C Library contained header files using statement expressions that lead to precisely this bug.)

Jumping into a statement expression with goto or using a switch statement outside the statement expression with a case or default label inside the statement expression is not permitted. Jumping into a statement expression with a computed goto (see Labels as Values) has undefined behavior. Jumping out of a statement expression is permitted, but if the statement expression is part of a larger expression then it is unspecified which other subexpressions of that expression have been evaluated except where the language definition requires certain subexpressions to be evaluated before or after the statement expression. In any case, as with a function call, the evaluation of a statement expression is not interleaved with the evaluation of other parts of the containing expression. For example,

       foo (), (({ bar1 (); goto a; 0; }) + bar2 ()), baz();

calls foo and bar1 and does not call baz but may or may not call bar2. If bar2 is called, it is called after foo and before bar1.

摘自GNU C中的语句表达式

时间: 2024-10-19 07:02:52

摘自GNU C中的语句表达式的相关文章

摘自GNU C中的typeof

6.6 Referring to a Type with typeof Another way to refer to the type of an expression is with typeof. The syntax of using of this keyword looks like sizeof, but the construct acts semantically like a type name defined with typedef. There are two ways

嵌入式C语言自我修养 03:宏构造利器 - 语句表达式

3.1 基础复习:表达式.语句和代码块 表达式 表达式和语句是 C 语言中的基础概念.什么是表达式呢?表达式就是由一系列操作符和操作数构成的式子.操作符可以是 C 语言标准规定的各种算术运算符.逻辑运算符.赋值运算符.比较运算符等.操作数可以是一个常量,也可以是一个变量.表达式也可以没有操作符,单独的一个常量甚至是一个字符串,也是一个表达式.下面的字符序列都是表达式: 2 + 3 2 i = 2 + 3 i = i++ + 3 "wit" 表达式一般用来数据计算或实现某种功能的算法.表

Sql语句报ORA-01795: 列表中的最大表达式数为 1000

错误信息:java.sql.SQLException: ORA-01795: 列表中的最大表达式数为 1000,错误信息如下: serviceid是:work -------------other WorkCmd-------------------------hz2 <2017-6-13 下午06时23分06秒 CST> <Warning> <Socket> <BEA-000449> <Closing socket as no data read f

JavaScript中的函数表达式

在JavaScript中,函数是个非常重要的对象,函数通常有三种表现形式:函数声明,函数表达式和函数构造器创建的函数. 本文中主要看看函数表达式及其相关的知识点. 函数表达式 首先,看看函数表达式的表现形式,函数表达式(Function Expression, FE)有下面四个特点: 在代码中须出现在表达式的位置 有可选的函数名称 不会影响变量对象(VO) 在代码执行阶段创建 下面就通过一些例子来看看函数表达式的这四个特点. FE特点分析 例子一:在下面代码中,"add"是一个函数对象

JSP中使用EL表达式

EL表达式 :EL 全名为Expression Language,就是为了替代<%= %>脚本表达式. EL主要作用: 获取数据: EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域中检索java对象.获取数据.(某个web域 中的对象,访问javabean的属性.访问list集合.访问map集合.访问数组) 执行运算: 利用EL表达式可以在JSP页面中执行一些基本的关系运算.逻辑运算和算术运算,以在JSP页面中完成一些简单的逻辑运算.${user==null} 获取web

关于 Java 中 finally 语句块的深度辨析

可不能小看这个简单的 finally,看似简单的问题背后,却隐藏了无数的玄机.接下来我就带您一步一步的揭开这个 finally 的神秘面纱. 问题分析 首先来问大家一个问题:finally 语句块一定会执行吗? 很多人都认为 finally 语句块是肯定要执行的,其中也包括一些很有经验的 Java 程序员.可惜并不像大多人所认为的那样,对于这个问题,答案当然是否定的,我们先来看下面这个例子. 清单 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1

C#中的Lambda表达式和表达式树

在C# 2.0中,通过方法组转换和匿名方法,使委托的实现得到了极大的简化.但是,匿名方法仍然有些臃肿,而且当代码中充满了匿名方法的时候,可读性可能就会受到影响.C# 3.0中出现的Lambda表达式在不牺牲可读性的前提下,进一步简化了委托. LINQ的基本功能就是创建操作管道,以及这些操作需要的任何状态.这些操作表示了各种关于数据的逻辑,例如数据筛选,数据排序等等.通常这些操作都是用委托来表示.Lambda表达式是对LINQ数据操作的一种符合语言习惯的表示方式. Lambda表达式不仅可以用来创

python中的问号表达式

我们知道C语句中存在问号表达式,例如下面: a=2>1? 10: -10; 那么,Python脚本语言中是否也存在类似的问号表达式呢?答案是肯定的.而且存在两种方式. 方式1:使用if else实现 如下例: b = 5 a = 2 if b == 4 else 1 print a 方式2:使用逻辑运算符实现 如下例: b = 5 a = b == 4 and 2 or 1 要注意:使用逻辑运算时要保证and后面的变量值不能为非,否则会出错. 提示 python中的and和or和其它语言的区别很

try catch finally中return语句与非return语句的执行顺序问题

finally语句一定是会被执行的,不管前边的try块catch块中有无return语句,并且如果finally中存在return语句,这个return语句将会是最后执行的return语句,即函数最后的返回值.try,catch中的return并不是让函数直接返回,而是return语句执行完毕,把返回结果放到函数栈中,转而执行finally块,所以,若是finally中含有return语句,那么函数栈中的返回值将被刷新.看以下几种情况: 1:try有return,finally无 public