To include a file once

Started by
15 comments, last by Zahlman 18 years, 11 months ago

#pragma once

and

#ifndef _FILENAME_H_
#define _FILENAME_H_

// Code here

#endif


They will give you the same result in most situtations but it doesn't take much imagination to come up with one that won't.


#include "headerfile.h"
#undef _FILENAME_H_
#include "headerfile.h"


Will yield different results for the two solutions.
Advertisement
If you really wanted to make sure an include file is only included once, you can do this:

// file.h#ifndef _FILENAME_H#define _FILENAME_H...#endif// file.cpp#ifndef _FILENAME_H   #include "file.h"#endif


Seems a little redundant, but I believe this is a programming gem somewhere that does help speed up the compiling process since the file is not pasted every time. Of course this may be what pragma once does though...not sure. However, I have had a few problems in the past with this, so I'm not sure how it was supposed to work, I need to find that source I saw it in.
Quote:Original post by Drew_Benton
If you really wanted to make sure an include file is only included once, you can do this:

*** Source Snippet Removed ***

Seems a little redundant, but I believe this is a programming gem somewhere that does help speed up the compiling process since the file is not pasted every time. Of course this may be what pragma once does though...not sure. However, I have had a few problems in the past with this, so I'm not sure how it was supposed to work, I need to find that source I saw it in.

The book Large Scale C++ Software Design by John Lakos advocates that technique but it's now generally considered to be a bad idea (see C++ Coding Standards by Herb Sutter and Andrei Alexandrescu for example). At the time it could offer significant performance improvements but as Lakos warns in the book it has the disadvantage that you have to be careful to make sure that the include guards match in the header file and in every place the header file is included which poses a potential maintenance headache. The performance issue is largely redundant now since all modern compilers are able to optimize for this case without the added complexity of external include guards.

Game Programming Blog: www.mattnewport.com/blog

That's something I never quite understood. Why can't the language, by default or with an option, have a mechanism to always include headers once?

That's quite an omission in my opinion. Having to come up with your own header guards, which can be used by something else, is a bit of a pain.

#include_once "blablabla.h"

not that hard, is it.

Everything is better with Metal.

Quote:Original post by mattnewport
The book Large Scale C++ Software Design by John Lakos advocates that technique but it's now generally considered to be a bad idea (see C++ Coding Standards by Herb Sutter and Andrei Alexandrescu for example).


Thanks a bunch! That was it. Ok then, so much for studying that book anymore [wink]. I'll have to put that C++ Coding Standards on my to buy list.
Quote:Original post by Drew_Benton
Quote:Original post by mattnewport
The book Large Scale C++ Software Design by John Lakos advocates that technique but it's now generally considered to be a bad idea (see C++ Coding Standards by Herb Sutter and Andrei Alexandrescu for example).


Thanks a bunch! That was it. Ok then, so much for studying that book anymore [wink]. I'll have to put that C++ Coding Standards on my to buy list.

Most of what the book has to stay is still good advice, in fact it's referenced quite often by C++ Coding Standards, it's just that bit of advice that's been superseded.

Game Programming Blog: www.mattnewport.com/blog

Quote:Original post by oliii
That's something I never quite understood. Why can't the language, by default or with an option, have a mechanism to always include headers once?


Because it's not a language mechanism; the preprocessor is "separate".

The real solution, of course, is to have some sort of real import model. This would probably have a huge ripple effect on the rest of the C++ grammar, but while the result might well be much nicer, it would completely destroy unbelievable masses of old code. (Personally, I don't particularly sympathise though >:) )

This topic is closed to new replies.

Advertisement