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.