开发者

How to overcome vc++ warning C4003 while writing common code for both gcc and vc++

开发者 https://www.devze.com 2022-12-21 15:56 出处:网络
I have a code that is compiled in both gcc and vc++. The code has a common macro which is called in two scenarios.

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.


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 :)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消