Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

#ifndef or #pragma once? 1

Status
Not open for further replies.

McBugzz

Programmer
Sep 17, 2002
90
0
0
JP
For more than 2 years I have used the folowing construction to prevent files from multiple including:

#ifndef BLA_BLA_BLA
#define BLA_BLA_BLA

// ...

#endif

Recently I descovered the

#pragma once

and even though this seems to be an easier statement, I don't recall EVER seeing it in ANY project, personal or company, big or small - never.

Why is #pragma once not being used all around as it is shorter?
 
#pragma once is a note to the compiler to only open the file once. This is basically to speed up compilation. It is not a standard C++ feature: it is just MSVC specific.

Most people try to keep to standard C++, even when the only products they produce are for the MS platform.
 
I also recently came across the #pragma once command when I had a multiple function definition error. When I eventually replaced the pragma with the #ifndef syntax, it solved the compiler problem. There is a functional difference between the two and it looks as though the #ifndef approach is safer.
 
I actually believe #pragma once was Metrowerks specific in the CodeWarrior compilers but was later adopted by Microsoft in VC++.
Also, when you add a new class to a VC++ project, the statement appears towards the head of the header file as such:
[tt]
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
[/tt]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top