More about statement expressions ...

Even More From Statement Expressions

Statement expressions allow compound statements to return a value.  Quite
useful. However, there are two small language extensions which, when used with
statement expressions, make them an even more general and flexible mechanism,
especially when used in conjunction with macros.

First, there is the "typeof" operator. Syntactically, this operator is similar
to the sizeof operator in that its operands are either types, variables, or
expressions. However, instead of returning the size of the operand, it returns
the type of the operand. Semantically, "typeof" behaves like a type defined
with a typedef. As such, it can be used anywhere a typedef can be used, e.g.,
in a declaration, a cast, as an operand to sizeof, or in a typedef
declaration.

For example, without "typeof", a max macro for an integer variable might be
written like this:

                #define min_int(x,y) ({         \\
                        int _x = (x);           \\
                        int _y = (y);           \\
                        _x < _y ? _x : _y;      \\
                        })

With "typeof", it can be rewritten in a generalized way as:

                #define min(x,y) ({             \\
                        typeof(x) _x = (x);     \\
                        typeof(y) _y = (y);     \\
                        _x < _y ? _x : _y;      \\
                        })

Second, there is an extension referred to as "local labels". Local labels
provide a mechanism for declaration and definition of labels that are local to
the enclosing block (including function scope). Statement expressions may
have local labels as well.

Local labels are declared as:

        __label__ <label_name>;

and must appear prior to any statements within a block. Also, the label
itself is defined in the usual way, i.e., with a

        <label_name>:

Local labels are convenient for those times when a macro using a compound
statement (or statement expression) is best written using a goto.  Using
normal labels, this would impossible, because every use of the macro would
define another instance of a label with the same name, causing the label
to be multiply defined. With local labels, that problem is solved because
each definition of a label is unique and exists only within the block it
is declared.

A typical example of how local labels can be used, that also incorporates the
use of statement expressions and the typeof operator, is by means of a search
macro. A search for an element of a one dimensional array could be written as
follows:

        #define SEARCH(array, value, max)                               \\
        ({                                                              \\
                __label__ found_it;                                     \\
                typeof (value) _value = (value);                        \\
                typeof (\*(array)) \*_array = (array);                    \\
                typeof (max) _max = (max);                              \\
                typeof (max) i, index;                                  \\
                                                                        \\
                for (i = 0; i < max; i++)                               \\
                        if (_array[i] == _value)                        \\
                        {                                               \\
                                index = i;                              \\
                                goto found_it;                          \\
                        }                                               \\
                index = -1;                                             \\
        found_it:                                                       \\
                index;                                                  \\
        })

Note:
        1) the use of the typeof operator to generalize the macro;
        2) the use of the label accessible \*only\* within the local block via
           the __label__ declaration;
        3) the use of the statement expression to return the desired value.

Comments:

Statement expressions sound cool. Are they implemenetd by gcc? Are they in Sun Studio 11? Are they proposed for incorporation into the next C standard? Will they be in a future version of Sun Studio? Are they something you just made up as an interesting idea? Inquiring minds want to know. (Actually some information along those lines should probably have been in the first paragraph of your blog.) Good Summary though!

Posted by Chris Quenelle on April 04, 2006 at 03:51 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

dpagan

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today