You recently asked about legitimate uses of template meta-programming.

This really boils down to phase-distinction and inlining. Let's assume 
that you have a rather complicated function of type:
    f: (int, t1, t2, t3, ... tN) -> (),
that calls several internal functions g1, ..., gN, and that the first 
argument is used to select different pieces of code, like:
    if a == 1 then
       (* Do one thing *)
    else if a == 2 then
       (* Do something else. *)
    end;
    g2 ();
    if a == 2 then
       (* Do something yet different. *)

In this case, using template meta-programming, I can force the compiler 
to generate several different versions of f, by instantiate templates 
e.g., f<1>, f<2>, ... f<M>, with the hope that inlining and constant 
expression elimination will improve the execution speed of the program 
(elimination of branches is very useful):

def f'(a : int, v1: t1, ..., vN : tN) =
    case a of
       1 : return f<1>(v1, ..., vn);
       2 : return f<2>(v1, ..., vn);
       3 : return f<3>(v1, ..., vn);
       else: return f<>(a, v1, ...., vn);

I think the phase-distinction in meta-template programming is sometimes 
an under-appreciated feature.

Thanks,

PKE.

-- 
Pål-Kristian Engstad ([EMAIL PROTECTED]), 
Lead Graphics & Engine Programmer,
Naughty Dog, Inc., 1601 Cloverfield Blvd, 6000 North,
Santa Monica, CA 90404, USA. Ph.: (310) 633-9112.

"It is better to have 100 functions operate on one data structure
 than 10 functions on 10 data structures." -- NN


_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev

Reply via email to