Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#pragma once Versus #if !defined MYHEADER_INCLUDED_ [duplicate]

Possible Duplicate:
#pragma once vs include guards?

What are the differences (in performance, usability and functionality) in using #pragma once and #if !defined MYHEADER_INCLUDED_ constructs? Or what is the difference between the two?

like image 689
bdhar Avatar asked Jun 03 '10 07:06

bdhar


2 Answers

Wikipedia has all your answers for this and is easy enough to find

From the article

In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation.

Thus, #pragma once serves the same purpose as #include guards, but with several advantages, including: less code, avoiding name clashes, and improved compile speed.

It has a more in-depth look at the advantages and disadvantages in the article. If you are really interested I suggest you read it completely, not just the blurb above.

like image 160
Dan McGrath Avatar answered Oct 11 '22 07:10

Dan McGrath


  1. performance -- usually the #pragma once implementation compiles faster, because it's intent is clear
  2. functionality -- both serve the same purpose -- to avoid more than one inclusion of a header file. The defined version has of course wider uses too, and if used carefully will allow to check in another file if the former was included (for conditional compilation for example)
  3. usability -- if portability is not an issue go with #pragma once -- however, this is a non-standard extension
like image 36
Kornel Kisielewicz Avatar answered Oct 11 '22 08:10

Kornel Kisielewicz