Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why no warning with "#if X" when X undefined?

I occasionally write code something like this:

// file1.cpp #define DO_THIS 1  #if DO_THIS     // stuff #endif 

During the code development I may switch the definition of DO_THIS between 0 and 1.

Recently I had to rearrange my source code and copy some code from one file to another. But I found that I had made a mistake and the two parts had become separated like so:

// file1.cpp #define DO_THIS 1 

and

// file2.cpp #if DO_THIS     // stuff #endif 

Obviously I fixed the error, but then thought to myself, why didn't the compiler warn me? I have the warning level set to 4. Why isn't #if X suspicious when X is not defined?

One more question: is there any systematic way I could find out if I've made the same mistake elsewhere? The project is huge.

EDIT: I can understand having no warning with #ifdef that makes perfect sense. But surely #if is different.

like image 527
Mick Avatar asked Oct 29 '09 13:10

Mick


People also ask

Why was there no warning for the 2004 Indian Ocean tsunami?

A spokesman for the disaster agency said the warning was canceled after the waves made land. Unconfirmed reports also claimed that sirens did not sound in some areas because they were left without power due to the initial earthquake and did not have a secondary source of energy.

What are 3 natural warnings?

Natural WarningsGROUND SHAKING, a LOUD OCEAN ROAR, or the WATER RECEDING UNUSUALLY FAR exposing the sea floor are all nature's warnings that a tsunami may be coming.

Did anyone know the 2004 tsunami was coming?

WARNING SYSTEMThose killed in 2004 received no formal warning of the approaching waves and had almost no chance to get out of the way. Since then, millions of dollars have gone into a vast network of seismic and tsunami information centers, setting up sea and coastal instruments and erecting warning towers.

Can you be warned of a tsunami?

Tsunami Warning – A tsunami warning is issued when a tsunami with the potential to generate widespread inundation is imminent, expected, or occurring. Warnings alert the public that dangerous coastal flooding accompanied by powerful currents is possible and may continue for several hours after initial arrival.


1 Answers

gcc can generate a warning for this, but its probably not required by the standard:

-Wundef
Warn if an undefined identifier is evaluated in an `#if' directive.

like image 174
Bill Avatar answered Sep 26 '22 21:09

Bill