Too many actual parameters for a macro?

the code:

#include <iostream>

using namespace std;

#define ADD(x,y)  ((x)+(y))

int main( int argc, char** argv )
{
    cout << ADD(1,2,) << endl;
    return 0;
}

Compiler Output:

1> Compilation ...
    1> main.cpp
    1> c: \ warn_test \ main.cpp (9): warning C4002: there are too many actual parameters for the "ADD" macro

Why is this not a mistake?

g++ (GCC) 4.2.1 20070719 [FreeBSD] gives a more reasonable (in my opinion) conclusion:

main.cpp: 9: 18: error: the macro "ADD" passed 3 arguments, but takes only 2
    main.cpp: In the function 'int main (int, char **)':
    main.cpp: 9: error: ADD not was announced in this area

Although I'm not quite sure what the compiler considers as the third argument.

EDIT: Added full version gccand version information.

+3
source
3

ADD(1,2,), ,. , !

@schnaader: , . .

[] . : g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5, :

test.cpp:9: error: macro "ADD" passed 3 arguments, but takes just 2
test.cpp: In function ‘int main(int, char**)’:
test.cpp:9: error: ‘ADD’ was not declared in this scope

[edit2] , :-). , . VS , g++. , - - .

0

, , .

, , visual studio ? :

#define MACRO(...) my_func(true, __VA_ARGS__);

MACRO(1,,2); // Missing argument
MACRO(1,); // missing tail
MACRO(); // no arguments

, . , .

+2

I assume this is a compiler choice option. If there was a third parameter, this would probably be more problematic, but like not, you can argue about just ignoring the comma or about an error. Microsoft seems to be more tolerant of errors (for example, in parsing IE HTML).

+1
source

All Articles