SUMMARY: Preprocessor Concatenation

From: Jochen Bern (bern@penthesilea.uni-trier.de)
Date: Mon Jul 15 1996 - 18:43:56 CDT


I wrote:
> I have a Need to allow C Source Code to take a Preprocessor Macro
> and a constant Text and have them concatenated by the Preprocessor.
> Works fine in gcc:
> #define REAL_CONCAT(a,b) a ## b
> #define CONCAT(a,b) REAL_CONCAT(a,b)
> #define MACRO Replacement
> CONCAT(MACRO,MoreText)
> In the SunOS 4.1.3_U1B bundled cc, I can concatenate constant Text OK,
> but I can't figure out how to do Macro Expansion simultaneously for
> the Life of me! Has anyone enough Experience, or Insight into the
> SunOS cc, to tell me how to get it to work?

SomeONE had: Torsten Metzner <tom@plato.uni-paderborn.de>. Using his
Example, I was able to come up with the Code below, which works both
under gcc and SunOS cc. However, it relies on nonstandard Features
of both and may break on other Platforms.

Yes, I am aware that the correct Way to deal with this Kind of Problem
is to switch to C++ and use Templates. :-}

Thanks again,
                                                                J. Bern

/* Run this through $(CC) -E | tail -8, respectively $(CXX) -E | tail -8,
   to see the Light :-) Tested for cc, gcc, g++, acc, and CC on SPARC/SunOS,
   SPARC/Solaris and x86/Solaris, except cc/x86/Solaris */

/* This goes into the general Header Files */

#ifdef __STDC__
#define LIT_CONCAT(a,b) a ## b
#define CONCAT_EXPAND(a,b) LIT_CONCAT(a,b)
#define CONCAT_SIMPLE(a,b) LIT_CONCAT(a,b)
#else
#define CONCAT_EXPAND(a,b) a(b)
#define CONCAT_SIMPLE(a,b) a/**/b
#endif

/* Topmost Header File defines the main Object we deal with */

/* This one is what we need */
#define DERIVED_NAME(c) CONCAT_EXPAND(c,main_obj)
/* This one provided for this Demo only */
#define NO_DERIVED_NAME(c) CONCAT_SIMPLE(c,main_obj)

/* The reusable Routines' Header File uses these
   to create Names for derived Variable Types */

/* These two are what we need */
#define struct_of___(c) struct_of___/**/c
#define Element DERIVED_NAME(struct_of___)
/* This one provided for this Demo only */
#define NoElement NO_DERIVED_NAME(Nostruct_of___)

/* Real Source Code follows */

Test 1: How the Type will be used. Should be always what we want
(struct_of___main_obj) with EXPAND
   EXPAND: Element
   SIMPLE: Noelement
Test 2: For Comparison: Fixed Text Concat. Should be always OK
("FrontRear") with SIMPLE
   EXPAND: CONCAT_EXPAND(Front,Rear)
   SIMPLE: CONCAT_SIMPLE(Front,Rear)

-- 
  /\  /""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""\
 /  \/ bern@uni-trier.de    (Size Limit!)   | P.O. Box 1203 | Ham:  \/\
/ J. \ bern@ti.uni-trier.de (SUNAttachm.OK) | D-54202 Trier | DD0KZ /  \
\Bern/ No Finger etc.; Use Mail (Subj. "##" for Autoreply List) and \  /
 \  /\ WWW. /\/
  \/  \____________________________________________________________/



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:11:05 CDT