¿Cuándo necesitamos #ifdef antes que #undef?

In many of the C and C++ files I have seen macros like this:

#ifdef X
#undef X  // no statements in between

I feel that, it's adequate to simply write:

#undef X

Si la macro X wasn't defined, then the #undef should have no effect.

Lo es ok to put standalone #undef, if I want to only indefinir a macro ? Does that make the coding practice bad in anyway ?

preguntado el 01 de febrero de 12 a las 03:02

I agree; I just did a quick search and this behaviour seems to be consistent across the major compilers. Perhaps some historically did not allow you to undef an undefined macro? Not sure. I'd say that it's probably safe. -

Seems to me that it is fine all by itself -

I think I remember atleast MSVC erroring out on me with that. -

@Xeo, maybe in the past. MSDN reckons that it's OK as of VS2005. -

@anthony: The docs actually say it's valid as far back as VS 2003. It's probably VC 6 he's thinking of, notorious for its lack of standards compliance. -

3 Respuestas

See ISO C99 paragraph 2.

Una directiva de preprocesamiento del formulario

# undef identifier new-line

causes the specified identifier no longer to be defined as a macro name. It is ignored if the specified identifier is not currently defined as a macro name.

Incluso Visual C ++ 6 (which was notorious for bad standards compliance) allows this:

También puede aplicar el #undef directive to an identifier that has no previous definition. This ensures that the identifier is undefined. Macro replacement is not performed within #undef Declaraciones.

Respondido 01 Feb 12, 08:02

I can confirm that this is exactamente lo mismo para ISO C ++ 11 también. - Antonio

I'm sure it's an artifact of history. As mentioned in jdigital's answer, the 2nd edition of K&R says

It is not erroneous to apply #undef to an unknown identifier.

However, that sentence is not in the 1978 edition. I'm pretty sure pre-standard compilers would often throw an error if you tried to #undef an undefined macro.

También el ANSI C Rationale says:

It is explicitly permitted to #undef a macro that has no current definition.

I'd imagine that if it was already universal practice, there would be no need to call it out in the rationale.

All that said, it's not necessary in modern code, but it doesn't really hurt either.

contestado el 23 de mayo de 17 a las 15:05

I have no evidence but I'd wager that the C compilers of that era (first edition) would not throw an error. It wouldn't fit with the Unix philosophy. - jdigital

How's this for obsessive: I managed to Google-up an old DeSmet-C compiler for MS-DOS (ver 2.4 from 1984!), and it throws an error when I #undef FOO -- 3 # $$ undef FOO / error:not defined - Michael Burr

You win! (MS-DOS people clearly didn't get the Unix philosophy.) - jdigital

@MichaelBurr: please post your comment as an answer. I'd like to give you some rep for this. - Ken Bloom

Kernighan and Ritchie (2nd edition) agree with you.

EDIT: quote from the source (section A12.3):

Una línea de control del formulario

# undef identificador

causes the identifier's preprocessor definition to be forgotten. If is not erroneous to apply #undef to an unknown identifier.

Respondido 01 Feb 12, 08:02

Can you quote from the source? - Antonio

@anthony-arnold: quoted from source as requested. - jdigital

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.