5 Generic programming is the concept of using the
6 same code with different types, where eventually the
7 code is expanded for each type requested.
9 C++ introduces the notion of template<>.
10 In lightweight C++, generic programming is achieved
11 with template macro classes and locallity of typedefs
15 0a. abstract template classes
16 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 Abstract classes are in reallity just macros expanded
19 with hierarchy into derrived classes. For example:
22 int foo () { return i++; }
29 In this example 'A' is the same as if it was declared:
32 int foo () { return i++; }
36 The class 'XX' is called abstract because it is not really
37 parsed until it derrives into a real class.
43 A typedef declared inside a class is local to the
44 class. Only declarations in the scope of the class
45 can use its name. Example:
59 Local typedefs are parsed before all other declarations.
62 1. Putting it together
63 ~~~~~~~~~~~~~~~~~~~~~~
65 With the two above combined we can make a sample
66 container template. Because every declaration in
67 a template class is abstract, it can use names not
71 // a template using a type 'X'.
75 template class container {
78 X &operator [] (int i) {
83 // this is abstract -- non existant
84 void container.reset ()
87 memset (values, 0, sizeof values);
91 // make a container of integers
94 class i_container : container {
99 // make a container of floats
102 class f_container : container {
107 // a container of container of integers
110 class ci_container : container {
111 typedef i_container X;
123 F [1] = F [32] = 3.14;
127 2. Unique Specializations
128 ~~~~~~~~~~~~~~~~~~~~~~~~~
130 lwc implements the keyword 'specialize'. The syntax of
133 specialize container { typedef float X; } Obj;
135 Which will declare an object called "Obj" and will specialize
136 the container template with floats. In a way this is equivalent
137 to an anonymous class:
143 But there is also a difference. With specialize, lwc will not
144 re-specialize the template if it has been specialized before
147 This is extremely useful for templates in templates where
148 the types depend on the outer types. For example:
150 template class TwoContainers {
151 specialize container { typedef T1 X; } Cont1;
152 specialize container { typedef T2 X; } Cont2;
155 specialize TwoContainers { typedef int T1, T2; } IntIntObj1;
157 specialize TwoContainers { typedef int T1;
158 typedef float T2; } IntFloatObj1;
160 Here, although there are three requests to specialize a container
161 with 'int's, lwc will only specialize it once, give it a special
162 name and use this name the next two times.
164 This is also helpful in the case of a container of containers:
166 specialize container {
167 typedef specialize container {
175 In a hierarchy of template classes, the most recent typedef
176 declaration wins. For example:
179 typedef int X; // default type unless overrriden
183 specialize A {} Obj1;
184 specialize A { typedef float X; } Obj2;
186 Here, Obj1 has an array of integers and Obj2 an array of floats.
192 This is still an experimental feature. Some weird corner cases may
193 not work as they have not been tested. If lwc complains or gives
194 incorrect code, send e-mail to the list.