tags:

views:

85

answers:

2

Hi,

is it possible to call function-like-macros with less that all the parameters in linux? actually doing this only generates a warning in Visual Studio (warning 4003) and unassigned variables replaces with "".

But compiling it using g++ generates an error in linux ("error: macro * requires ** arguments, but only ** given").

is there any possible way to disable this or overcome it?

+2  A: 

The number of arguments in a macro invocation must exactly match the number of parameters in the macro definition. So, no, you cannot invoke a macro with fewer arguments than it has parameters.

To "overcome" it, you can define multiple differently named macros with different numbers of parameters.

C++0x (which is not yet standard, but which your compiler might partially support) adds support for variadic macros which can be called with a variable number of arguments.

James McNellis
GCC supports variadic macros since a long time. I don't know if they work the same as in C++0x though. He should give them a tryhttp://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.htmlHere it says that variadic macros come from C99. Shouldn't them be in C++ too ?
asr
@asr: Variadic macros were added to C in C99 (in 1999); the current C++ standard was approved in 1998 and does not have variadic macros. They have been added to the forthcoming C++0x. gcc might support them when compiling C++ code, but if it does it is as a language extension or as preemptive support for C++0x.
James McNellis
+1  A: 

The standard (§16.3 - Macro replacement) is clear that you have to pass the same number of arguments:

"If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no preprocessing tokens) in an invocation of a function-like macro shall equal the number of parameters in the macro definition."

I don't know of any g++ option to override this.

Matthew Flaschen