views:

194

answers:

2

I have a code that is compiled in both gcc and vc++. The code has a common macro which is called in two scenarios.

  1. When we pass some parameters to it.
  2. When we don't want to pass any parameters to it.

An example of such a code is:

#define B(X) A1##X

int main() {
        int B(123), B();

        return 0;
}

The expect output from the pre-processing step of compilation is:

int main() {
        int A1123, A1;

        return 0;
}

The output for both gcc and vc++ is as expected, but vc++ gives a warning:

warning C4003: not enough actual parameters for macro 'B'

How can I remove this warning and yet get the expected output?

Thanks.

+1  A: 

This might work depending on your VC++ version, etc

#define B(...)  A1##__VA_ARGS__

I don't know if vc++ will like an empty va args but its worth a shot - let me know if that works :)

Sam Post
Thanks Sam for the response. A am not to familiar with VA_ARGS in macros, and would like to go over the literature before I can apply this solution.Unfortunately I don't get much time to work on warnings when I have other pending tasks. I will get back to you soon.Thanks again.
compbugs
Sure thing, good luck and let me know if this works or if you have any further questions.
Sam Post
A: 

For Visual C++ you need to use the #pragma warning directive. The warning you are getting is C4003 (C => Compiler), 4003 => warning number.

#pragma warning (disable: 4003)

#define B(X) A1##X

int main() {
        int B(123), B();

        return 0;
}

Not sure about GCC, but I suspect you can opt to not define this pragma for GCC and suppress the warning (if any some other way).

Stephen Kellett
Thanks Stephen for this solution, I was aware of this and forgot to mention it in the question.This is one of the last solution (if everything else fails) that I am thinking of, I would like something that will remove this error, not suppress it.Thanks again.
compbugs