Karim Belabas on Wed, 14 Jun 2006 18:34:01 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

do {...} while(0) macros


Hi all,

  in order to emulate templates in ANSI C, some of the pari code uses the
(standard, ugly) macro construct:

  #define foo  do { A; B; } while (0)

The point is 
-- 1) to get a block in which to declare global variables
   [ which A, B would not provide ]
-- 2) #define foo2  A; B   is broken since 

    if (test) foo2

is interpreted as

    if (test) A;
    B;

which is not the intended behaviour

-- 3) #define foo3 { A; B; }  is broken since

    if (test) foo3; else whatever

is interpreted as

    if (test) { A; B; };
    else whatever      ^------ BAD


Today, Henri Cohen has found yet another way to break the above foo
definition, which I had thought was more or less foolproof:

   for (foo ;;)  / for ( ;foo; ) / for (;; foo)

don't work since one is not allowed to use loops within initializer,
termination test or modifier parts of a 'for' statement.

This is especially annoying for the (deprecated) addsiz functions which
one might conceivably find in code such as

    GEN i;
    for ( i = N0; cmpii(i, N) < 0; addsiz(i, 1, i) )

( Which is inefficient and should be written differently, as all code
using the <fun>z functions, but this is beside the point. )


The simplest "solution" seems to generate most of paricom.h automatically
from a template [ see the "OPERATION BY VALUE" part ].

A seemingly better solution is to kill all (obsolete, inefficient,
difficult to use) <fun>z functions in pari-2.5.

An intermediate solution would be to simply document the problem (say as
a FAQ item) and forget about it.

Any idea ? 

Cheers,


    K.B.
-- 
Karim Belabas                  Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1          Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]