🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #37

macros: "same name/different number of arguments"

#37
From: Hermann Stamm
Date: Fri, 30 Oct 1992 10:36
77 lines
2640 bytes
Hello!

The C-preprocessor has the disadvantage of not allowing
the same macro-name for macros of different number of
arguments.
Below you can see a little example for the need of this
possibility (e.g. the two functions _expand (one for
circles and one for rectangles) need either 2 or 3 
arguments). 
Below you can see my proposition for fixing this kind
of problem: 

  Define a macro which expands to a function-call
  with one open bracket but without the closing one.
  This "allows" a variable number of arguments; the
  only disadvantage is, that you have to write one
  more closing than opening bracket in the program.

My questions now are:

  - Is this portable/ANSI-conform ?

  - Are there any reasons for not doing it this
    way (style, ...) ?

  - What is the better method if more than one
    argument is the same for all function-calls
    (the X and L in the below example), the one
    used for CONTRACT or the one used for EXPAND ?

The main reason for not using the variable number
of arguments operator ,... of functions is the 
loosing of type-checking in the variable positions;
here type-checking is done at each position!

I hope for a lot of answers/suggestions.

Thanks in advance,

   Hermann.

---------------------------------------------------------------
contract(circle C, int l)            ==> contract circle C's 
					 radius by length l.

contact(rectangle R, int lx, int ly) ==> contract rectangle R's
					 width by lx and its 
					 height by ly.
expand ==> similar to contract
---------------------------------------------------------------

#define CONTRACT(X) (X)->class->_contract((X)
#define EXPAND(X,L) (X)->class->_expand((X),(L)

circle C;
rectangle R;

CONTRACT(C),8)
CONTRACT(R),8,9)

EXPAND(C,8))
EXPAND(R,8),9)

----------------------------------------------------

The Preprocessor expands the above text to the
following:

circle C;
rectangle R;
 
 ( C ) -> class -> _contract ( ( C ) , 8 )
 ( R ) -> class -> _contract ( ( R ) , 8 , 9 )
 
 ( C ) -> class -> _expand ( ( C ) , ( 8 ) )
 ( R ) -> class -> _expand ( ( R ) , ( 8 ) , 9 )
Technical Details
Message-ID: <1992Oct30.103610.24927@olymp.informatik.uni-bonn.de>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!Germany.EU.net!olymp!uran!hermann

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!Germany.EU.net!olymp!uran!hermann
From: hermann@uran.informatik.uni-bonn.de (Hermann Stamm)
Subject: macros: "same name/different number of arguments"
Message-ID: <1992Oct30.103610.24927@olymp.informatik.uni-bonn.de>
Sender: usenet@olymp.informatik.uni-bonn.de
Reply-To: hermann@uran.informatik.uni-bonn.de (Hermann Stamm)
Organization: Praktische Informatik III
Date: Fri, 30 Oct 1992 10:36:10 GMT
Lines: 79