Browse Prior Art Database

Handling varying number of parameters through a single macro definition in C++

IP.com Disclosure Number: IPCOM000030396D
Original Publication Date: 2004-Aug-10
Included in the Prior Art Database: 2004-Aug-10
Document File: 3 page(s) / 11K

Publishing Venue

IBM

Abstract

This disclosure details handling of varying number of parameters through a single macro definition in C++, without compromising the capability of enabling or disabling the macro at compile time.

This text was extracted from a PDF file.
This is the abbreviated version, containing approximately 53% of the total text.

Page 1 of 3

Handling varying number of parameters through a single macro definition in C ++

This disclosure details an outline of creating a single macro that caters to any number of parameters and can be switched on and off like a regular macro to either include or exclude a code block.

Following are the advantages of employing this idea:
1) One macro to represent a code block that can be parameterized using various combination and number of parameters, instead of multiple macros.
2) Smaller source code, thus providing advantages such as maintainability, manageability of codebase.
3) It allows better exploitation of features like function overloading in C++, directly.
4) It allows programmers to use a single macro name consistently rather than using different macro names for different combination or number of parameters.

The new solution that is being detailed below allows programmers to cater to any number of parameters with one macro, which can be switched on or off at compile time, by using C++ style single line comments to switch off the macro definition when not required (instead of conventional mechanisms of defining a macro to a blank in a macro definition to support switching the macro off), as represented below.

#IFDEF __FLAG__

    #DEFINE NAME SomeFunctionName
#ELSE

    #DEFINE NAME //SomeFunctionName
#ENDIF

Using the above style of writing a macro in C++, programmers can write a simple debugging utility as follows:

#IFDEF __DEBUG__

    #DEFINE DEBUG printf
#ELSE

    #DEFINE DEBUG //printf
#ENDIF

The above macro block when used as follows in the source code;

DEBUG( "This is a simple log statement" ) ;

DEBUG( "%s This is a simple log statement with one parameter",
a ) ;

DEBUG( "%s %s This is a simple log statement with two
parameters", a, b ) ;

Will expand to either

printf( "This is a simple log statement" ) ;

printf( "%s This is a simple log statement with one
parameter", a ) ;

printf( "%s %s This is a simple log statement with two
parameters", a, b ) ;

1

Page 2 of 3

or

//printf( "This is a simple log statement" ) ;

//printf( "%s This is a simple log statement with one
parameter", a...