so@brownie.cs.wisc.edu (Bryan So) (07/14/90)
I have a tricky way to write generic routines in C, I make use of
the paste operator (##) to paste the target type to the data type
and operations. For example, an array of integers can be declared
by
array(int) a;
and is implemented by
arrayint a;
The problem is the implementation below only works on Turbo C 2.0
and Turbo C++. I try it on Microsoft C 5.0 (sorry this is the only
version I have) and BSD UNIX. It does not compile. The BSD pre-
processor shows that the line marked with "PROBLEM" is translated to
arrayT instead of arrayint.
Is my method non-standard? Will Microsoft C 6.0 handle it?
Bryan
__________________________________________________________________________
(main.c)
#define T int
#include "array.h"
#undef T
main()
{
array(int) a;
}
__________________________________________________________________________
(array.h)
#if defined(BSD)
#define name2(x,y) _name2(x,y) /* BSD way to paste */
#define _name2(x,y) x\
y
#else
#define name2(x,y) _name2(x,y) /* Need extra redirection, see */
#define _name2(x,y) x##y /* Turbo C++ include/generic.h */
#endif
#define array(x) name2(array,x)
typedef struct {
int size;
T *vector; /* T to be defined in source file */
} array(T); /* PROBLEM */
/* ... other generic array operations ... */
__________________________________________________________________________