views:

145

answers:

1

In this maximally clipped source example, the manifest constant FOOBAR is being redefined. This is deliberate, and there is extra code in the live case to make use of each definition.

The pragma was added to get rid of a warning message, but then a note appeared, and I don't seem to find a way to get rid of the note.

I've been able to modify this particular source to #undef between the #define, but I would like to know if there's a way to inhibit the note without requiring #undef, since there are multiple constants being handled the same way.

#pragma warning( disable : 4005 ) // 'identifier' : macro redefinition
#define FOOBAR FOO
#define FOOBAR BAR

The compiler banner and output are as follows

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

message.c
message.c(3) : note C6311: message.c(2) : see previous definition of 'FOOBAR'
+6  A: 

You are not allowed to redefine a macro unless the new definition is identical to the current definition (if you redefine a macro and the new definition is different than the current definition, the program is actually ill-formed).

#undefing the macro before redefining it is the correct thing to do in this case:

#undef FOOBAR
#define FOOBAR FOO


#undef FOOBAR
#define FOOBAR BAR

Note that you are allowed to use #undef even if a macro name isn't currently defined, so there's no reason to test whether the macro is defined using #ifdef before using #undef on it.

James McNellis
I know from N1124, 6.10.3 / 2 says exactly that. Since the C compiler only issues a warning which can be inhibited, I was hoping there was another method to inhibit the following note.
piCookie
@piCookie: Why would you want to suppress this warning? The compiler is telling you that you code is _wrong_. The code should be fixed, especially since the fix is really easy.
James McNellis
Maybe he doesn't know about `#ifdef`
Johannes Schaub - litb
@Johannes: You are allowed to `#undef` a macro name even if the macro isn't defined, but _that_ might not be common knowledge. I've added an example demonstrating that; thanks.
James McNellis
@James, ah haven't known. Thanks for enlighting me.
Johannes Schaub - litb
@Johannes: I must not use #ifdef because I actually need each definition in order. @James: As to why I would want to suppress it; I had already implemented the correct fix with #undef before posting the question. However, since the compiler does not treat this as an error condition, but an ignorable warning instead, I wanted to use the compiler to suppress the note as well. I have examined the code and know that each definition in order is what I want. I would like to know if it is possible also to suppress the note. Perhaps more generally, is it possible to suppress notes?
piCookie
@piCookie: I don't know how to suppress notes if you can't do it with `#pragma warning(disable:...)`, however, upgrading to a more recent version of Visual C++ would solve your problem. Visual C++ 2010 does not emit this note you are seeing. I no longer have older versions installed on this PC, but I can check 2003/2005/2008 at work tomorrow.
James McNellis