tags:

views:

144

answers:

4
#undef GOOGLE_LONGLONG
#undef GOOGLE_ULONGLONG
#undef GOOGLE_LL_FORMAT

#ifdef _MSC_VER
#define GOOGLE_LONGLONG(x) x##I64
#define GOOGLE_ULONGLONG(x) x##UI64
#define GOOGLE_LL_FORMAT "I64"  // As in printf("%I64d", ...)
#else
#define GOOGLE_LONGLONG(x) x##LL
#define GOOGLE_ULONGLONG(x) x##ULL
#define GOOGLE_LL_FORMAT "ll"  // As in "%lld". Note that "q" is poor form also.
#endif

Why do this and when to do such things?

A: 

To make sure they haven't been somehow defined already. Otherwise, the preprocessor may give warnings/errors when it redefines them a second time.

Shaggy Frog
Technically, it's the preprocessor giving the errors, not the compiler. But, yes, it should fail if you try to redefine without first undefining.
Steven Sudit
This is a little misleading; your answer could be interpreted to mean that it prevents previous definitions, but really it's to deal with their existence by silently ignoring them.
jamesdlin
@Steven Sudit: It should fail only if the two macro definitions are different.
jamesdlin
@james: You're correct. However, you would have to somehow know that any previous definition matches your current one, and if you knew that then you probably wouldn't be redefining it now. So the only *safe* way is to #undef first.
Steven Sudit
+2  A: 

Its just for the sake of security or we can say for the sake of precautions that we undefine macros before defining them,so that the compiler will not show any warning/errors on redefining them

Ankur Mukherjee
That's like saying catching all exceptions and then silently ignoring them is for security. This kind of thing doesn't make any sense. More likely, a mistaken desire to suppress useful errors/warnings.
Michael Aaron Safyan
A: 

Also in such cases where the values will be different but the variable remains the same.

For e.g

#define pi 3.1456

and down the lane you might just need pi as 3.14. So what you can do is,

#undef pi
#define pi 3.14

From an example here,

Any occurrences of the identifiers that follow these #undef directives are not replaced with any replacement tokens. Once the definition of a macro has been removed by an #undef directive, the identifier can be used in a new #define directive.

liaK
+4  A: 

Generally this is a bad idea. Ironically, given that you have "Google" in your symbol names, you might be curious to know that Google's C++ Style Guide urges against undefining macros before defining them. Basically, if you define a macro multiple times, you will get an error. The undef prevents these errors, which can suppress some alarm bells that probably should be going off. There are a few cases where an undef makes sense, such as in defining assert where the behavior of the macro can be different each time you include the header (based on whether some other macro is defined). If the macro should have a single value for the entire program, though, this undef doesn't make any sense.

Michael Aaron Safyan