#if in C++

May 12, 2010 at 6:59am
What is the purpose of #if in C++? How does it differ from the regular if? Why use one over the other?
May 12, 2010 at 7:01am
#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.
May 12, 2010 at 8:19am
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 May 12, 2010 at 8:26am
May 12, 2010 at 11:11am
May 12, 2010 at 1:39pm
#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.
May 12, 2010 at 2:13pm
#pragma - Demons fly out of your nose.
May 12, 2010 at 2:40pm
I really hate it when I'm reading someones code and see #pragma once.
May 12, 2010 at 2:43pm
closed account (z05DSL3A)
Why?
May 13, 2010 at 8:52am
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
???
May 13, 2010 at 11:19am
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.
May 13, 2010 at 12:17pm
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.
May 13, 2010 at 12:28pm
You don't need the #pragma once though, do you? What's the point of both?
May 13, 2010 at 12:39pm
Thats exactly my point "What's the point of both?"
May 13, 2010 at 12:40pm
TheTSPSolver: Google "demons fly out of your nose".
May 13, 2010 at 1:49pm
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....
May 13, 2010 at 2:22pm
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?
May 13, 2010 at 2:41pm
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 May 13, 2010 at 2:44pm
May 13, 2010 at 2:47pm
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.
May 13, 2010 at 2:50pm
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?
May 13, 2010 at 3:24pm
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 May 13, 2010 at 3:25pm
Topic archived. No new replies allowed.