I have been wodnering the difference between using:
#ifndef _SOMETHING_
#define _SOMETHING_
#endif
and the newer:
#pragma once
I read up on it on wikipedia and it seems to indicate that #pragma once is better for performance reasons. Visual C++ automatically inserts #pragma ocne if you create a new class which would indicate that microsoft agrees.
Yet i see lots of people still using the #ifndef method. is this just out of habbit? or is there some reason its better?
Which do you use? and why?
#ifndef vs #pragma once
-
- Posts: 1029
- Joined: Thu Apr 06, 2006 12:45 am
- Location: Tennesee, USA
- Contact:
-
- Admin
- Posts: 14143
- Joined: Wed Apr 19, 2006 9:20 pm
- Location: Oldenburg(Oldb), Germany
- Contact:
Well, the compiler can skip the whole file when reaching the pragma position, while the ifndef solution requires the compiler to parse the whole file - usually finding just the matching endif right at the end of the file. However, you could (but should not ) add something behind the endif which would be compiled. And since the compiler (in fact the preprocessor) has to parse the code to some extent it might be quite slow, in addition to the I/O overhead.
I always hated the #pragma directive. Good old #ifndef always works.
Reminds me of a great strip a friend sent me: http://www.hackles.org/cgi-bin/archives.pl?request=52
Reminds me of a great strip a friend sent me: http://www.hackles.org/cgi-bin/archives.pl?request=52
Always been a #ifndef user. But now that you mention it #pragma once might be faster. For hobby stuff, I only ever use VS for Windows so might as well gradually switch.
On a side note, I've made it a habit to use
Instead of adding files to the .vcproj. Makes life easier when sharing code around between projects.
On a side note, I've made it a habit to use
Code: Select all
#pragma comment( lib, "mylib.lib" )