#if in C++

What is the purpose of #if in C++? How does it differ from the regular if? Why use one over the other?
#if is a preprocessor directive, it says if this defined then compile this code, otherwise this code won't be compiled.

But just if it's run-time check.
Denis isn't it #ifndef you mean? #ifndef checks if a symbolic name is defined with #define
#if is used to test a symbolic name fx

1
2
#if SYS_NO == 1.0
//do something 


You can also test if it is larger or smaller than a value. All the same test as a normal
if. But #if is just used to test symbolic names

Last edited on
#ifdef = If whatever is defined then do this.
#ifndef = If whatever is NOT defined then do this.
#if = If this condition is met then do this.
#elif = Else if this condition is met do this.
#define = This really means that.
#undef = This no longer means anything.

There are a few more and you should read the link by moorecm but for the casually curious who don't want to read the article, and won't read it anyway, these are some poor summeries.
#pragma - Demons fly out of your nose.
I really hate it when I'm reading someones code and see #pragma once.
closed account (z05DSL3A)
Why?
Thanks or the link moorecm.

@Computergeek01: According to what you have said
#if = If this condition is met then do this.


Then why use #if instead of regular if.

@helios: Sorry I'm new to these forums so I don't get the joke.
#pragma
???
Because when ever I see pragma once I always see #ifndef and #define with it. Drives me insane. So I just hate it when I see #pragma once.
closed account (z05DSL3A)
I take it you mean somthing like this:
1
2
3
4
5
6
7
8
9
10
#pragma once
#ifndef GRANDFOO_H
#define GRANDFOO_H
 
struct foo
{
    int bar;
};
 
#endif /* GRANDFOO_H */ 


This is a safe way of doing it.
You don't need the #pragma once though, do you? What's the point of both?
Thats exactly my point "What's the point of both?"
TheTSPSolver: Google "demons fly out of your nose".
closed account (z05DSL3A)
#pragma once and #ifndef... do not do entirely the same thing.

As #pragma once is not guaranteed to be be implemented in any given compiler, you have a fallback to #ifndef....
Sorry what you said confuses me a little. What is the difference then? I know not all compilers work with #pragma once. But don't they both inevitably do the same thing?
once the compiler sees "#pragma once" it will immediately stop parsing the file (provided #pragma once is supported).

On the other hand, once the compiler sees "#ifndef" it will continue parsing the file looking for the matching #endif.

Therefore #pragma once is a bit faster.

But since it's not standard, there's no guarantee it will be supported, so the #ifndef/#endif check can be a fallback.

Which is why it's benefitial to use both.
Last edited on
closed account (z05DSL3A)
#pragma once: This tells the compiler that the header should only be processed once. the compiler keeps track of the header file name and if it comes across another #include for that header file it can move on without even opening the file.

#ifndef... (Include guard): The file is opened and processed. the guard is put in place. The next time the header file is included, the file is still opened, the guard is processed and the content is skipped until the corresponding #endif .

So, yes they inevitably do the same thing but #pragma once is supposed to do it quicker (reducing compile time). In a large project this can be significant.

Disclaimer:
I am not advocating the use of #pragma once but if you do use it it is better to use the 'standard' include guards as well.

I am also aware that some compilers are designed to recognise the 'standard' include guards and make every effort to increase there compile times acordingly.
Well that makes a lot more sense now. So realistically then from now on (considering I only use visual studio) I should use both then?
I couldn't find anyone in this thread explaining why the heck one would want to use #if , so I'll just go and say it's useful for checking what something is defined as.

EDIT: At compile time.

-Albatross
Last edited on
Topic archived. No new replies allowed.