Article View: comp.os.msdos.programmer
Article #1577ANSI C Paste Operator
From: Bryan So
Date: Sat, 14 Jul 1990 03:11
Date: Sat, 14 Jul 1990 03:11
52 lines
1980 bytes
1980 bytes
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 ... */
__________________________________________________________________________
Technical Details
Message-ID:
<10825@spool.cs.wisc.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!att!rutgers!uwvax!brownie.cs.wisc.edu!soShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!att!rutgers!uwvax!brownie.cs.wisc.edu!so From: so@brownie.cs.wisc.edu (Bryan So) Newsgroups: comp.os.msdos.programmer Subject: ANSI C Paste Operator Message-ID: <10825@spool.cs.wisc.edu> Date: 14 Jul 90 03:11:31 GMT Sender: news@spool.cs.wisc.edu Organization: U of Wisconsin CS Dept Lines: 52