To include a file once
To make sure a header is only included once I've always used
#ifndef _FILENAME_H
#define _FILENAME_H
... code ...
#endif
Though I have also seen #pragma once used. What's the difference between the two? Is one way out dated now? Or is it just preference?
As far as i'm aware the #ifndef etc is C/C++, whereas #pragma is a Microsoft Visual C++ directive that is not in the actual language. Both do the same thing but porting code to Linux with #pragma in won't work.
ace
ace
Additionally, #pragma once improves compilation peformance under MSVC (because the compiler can make assumptions about the way it handles the header).
Of course, GCC does the same optimisation when using normal include guards (#ifndef/#define), although MSVC dosn't.
Of course, GCC does the same optimisation when using normal include guards (#ifndef/#define), although MSVC dosn't.
Quote:Original post by Andrew Russell
Additionally, #pragma once improves compilation peformance under MSVC (because the compiler can make assumptions about the way it handles the header).
You obviously know this, but it isn't that the compiler 'can' make assumptions it's because it 'chooses to' make the assumptions. It could very well make those assumptions without #pragma once being used.
Just being picky
(c:
Quote:Original post by ace_lovegroveIt works if you're compiling with GCC (although earlier versions did emit a warning message) or ICC.
As far as i'm aware the #ifndef etc is C/C++, whereas #pragma is a Microsoft Visual C++ directive that is not in the actual language. Both do the same thing but porting code to Linux with #pragma in won't work.
Another newer and standard pattern to use is:
#if !defined(_FILENAME_H_)#define _FILENAME_H_// Code here#endif
Isn't underscore then uppercase reserved for the compiler's use?
At the company I work, the standard is to use the company's abbreviated name the the filename, like CO_FILENAME
For personaly projects, I use a project prefix, the filename, then 'h':
LIB_Threading_H
At the company I work, the standard is to use the company's abbreviated name the the filename, like CO_FILENAME
For personaly projects, I use a project prefix, the filename, then 'h':
LIB_Threading_H
Quote:Original post by Rob Loach
Another newer and standard pattern to use is:#if !defined(_FILENAME_H_)#define _FILENAME_H_// Code here#endif
Why is that a standard pattern as opposed to:
#ifndef _FILENAME_H_#define _FILENAME_H_// Code here#endif
Am I missing something?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement