2 Generic programming with auto functions
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 Auto-functions, as we already know, are a very powerful feature
6 of lwc. The code expansion can be easilly applied to achieve
7 generic code: the same code used with different *types*.
9 As of lwc 1.2, auto functions are redefined if they reference
10 local typedefs. For example:
34 In class B, the auto function 'a()' is redefined and it uses
35 the X2 type for its definition. So A.a calls X1.compare and
36 B.a calls X2.compare and therefore we avoid the overhead of
37 the virtual table in classes X1 and X2.
39 Furthermore, local typedefs can be used in the prototypes (return
40 value and arguments) of auto functions.
54 Which has an effect similar to C++'s covariant return types although
55 it doesn't always work with virtual functions.
57 Still, 'template classes' are more generic since they allow the definition
58 of generic data members. (see container)
60 ------------------------
63 The ability to have pure non-virtual functions and generic programming,
64 comes naturally with the existance of auto-functions. It is about
65 compile-time polymorphism. Our goal is to use compile-time polymorphism as
66 much as possible and leave virtual calls in just the necessary places.
68 In this spirit we could also implement:
70 - Pure local typedefs. For example:
74 auto X *a () { /* ... */ }
81 where no code is generated for A.a() because it uses a pure typedef.
88 auto void a () { /* uses this->data member */ }
89 void foo () { /* doesn't use anything generic */ }
96 where the data member 'data' is really created in class B and no code is
97 emitted for A.a() because it uses this abstract data member. With this
98 addition, we can do almost as much as template classes.