Dismiss
InnovationQ will be updated on Sunday, Oct. 22, from 10am ET - noon. You may experience brief service interruptions during that time.
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. Existing solution to solve the problem of handling varying number and combination of parameters in C++ code use a number of macros with predefined parameters and preprocessing directives such as #IF, #IFDEF, #IFNDEF, #ELIF etc, that are controlled using a preprocessing flag to switch on and off the code blocks implemented through macros. Drawback of such macros is that multiple macros need to be provided for various parameter combinations to be passed, in order to provide support for switching them on and off. This over a period of time can become difficult to maintain as similar code blocks are used to implement such combination of macros. For example, if a debug or a log macro is used in an application - multiple parameterized versions of the macro are needed to cater for varying number of parameters that a program may require, The reason for writing multiple macros is to switch a macro on or off implying either it maps to a code block or it eliminates the concerned code block depending upon compilation flags. For example: #IFDEF __FLAG__ #DEFINE DEBUG_1(X) printf( "%s", X ) #DEFINE DEBUG_2(X, Y) printf( "%s %s", X, Y ) #ELSE #DEFINE DEBUG_1(X) #DEFINE DEBUG_2(X, Y) #ENDIF and so on depending upon the number of parameters to be supported. The drawback being, there are a large number of macros to write and it still doesn't provide a solution for a varying number of parameters. On top of it, each macro has a different name and thus programmers need to remember and exercise caution to use the right macro in a given context. The new solution that is being detailed below allows programmers to create 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, thus allowing a much better use of C++ features such as function overloading.

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...