5 Also in 1.0: regular expressions and ctors of arrays
7 1. Postfix functional operator
8 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 'postfix' is a reserved word. The syntax of 'postfix' is
11 like a function and it has the effect that: arguments are
12 evaluated from left to right, the return value is the leftmost
13 argument (the first one evaluated). So postfix can remember
19 // typical useful case
20 p = postfix (p->next, free (p));
21 // .. or if ~ is overloaded to call free() on pointers
22 p = postfix (p->next, ~p);
24 // increment y by 23 after using the value
25 postfix (foo (y), y += 23);
27 foo (postfix (y, y += 23));
30 return postfix (do_calculate (buffer), free (buffer), release_semaphore ());
33 x = postfix (strdup (y), free (x));
36 postfix (postfix (x, x -= 10), z [x] = 0);
39 postfix ((A, B), (C, D));
41 Generally, postfix is a very cool thing and opens a whole
42 new chapter of possibilities in C expressions and statements.
48 This is mainly useful to avoid the backslash nightmares when
49 passing regular expression recipes to regexp libraries.
50 In order to pass the regexp "\w\s\"\\", we'd have to write
51 the string "\\w\\s\\\"\\\\". A nightmare.
53 String literals where a 'r' is placed right before the opening
54 double quote, don't have this problem since the lwc preprocessors
55 will escape all backslashes in them. For example:
57 char msg [] = r"\w\s\"\\";
59 will be converted by lwc to:
61 char msg [] = "\\w\\s\\\"\\\\";
63 and after the C compiler sees it, will be converted back to "\w\s\"\\"
66 3. Single quoted strings
67 ~~~~~~~~~~~~~~~~~~~~~~~~
69 Because single quotes are fun, it's possible to have string literals
70 in single quotes. The way to distinguish between a string and a character
71 constant is the number of letters. Single-quoted strings must have more
72 than 4 characters. Example:
74 char msg [] = 'Hello World!';
75 char x [] = '"What do you want?", said Elwood';
76 char y [] = 'a'; // WRONG!! 'a' remains 'a'
82 References have been implemented and can be applied on global and local
83 variables, structure members, function arguments and virtual variables.
84 References are "pointers in disguise", they are an alias for a variable.
85 Whatever we do on references happens on the variable they're pointing
91 // ri is an 'alias' of i
105 In lwc it is possible to reassign references. Normally, this is impossible
106 -as in C++- because the operations on references always happen for the
107 things they're pointing at and references can be set only once at their
108 initialization. Lwc implements a new unary prefix operator called
109 'dereference'. When 'dereference' is applied on an expression, all
110 references in it are treated as pointers. Extending the previous example:
116 // make ri point to j
117 dereference (ri) = &j;
119 // assign reference to reference
120 dereference (ri = rj);
122 dereference ri = dereference rj;
125 The best way to understand all these are by compiling the above code and
126 looking at the output. 'dereference' is needed since in lwc we don't have
127 C++'s initialization lists at the constructors of objects. So the only
128 way to initialize reference members of objects is with the use of
129 dereference in the ctor.
131 The exception is structure arguments. Because lwc passes all structures
132 by reference anyway (and adjust that pointers and objects can be passed),
133 declaring references of structures in function arguments, is obsolete,
134 untested and will confuse the lwc preprocessor in function overloading.
136 5. Overloaded operators on 'this'
137 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139 See 4.2-operator-overloading.
140 Generally, if for example, the [] operator is overloaded
141 for a class, the expression:
145 will invoke operator overloading and not pointer displacement.