Sunday, February 17, 2008

#pragma once standardized?

I have at a few places, where people have mis-heard/mis-read that that #pragma once has been standardized for C++0x. It's "not".

Even gcc has it labelled as an obsolete feature (for a couple of years now) - Obsolete once-only headers.

Since, it is not standard in the first place, one should avoid using it. The standard include guards "#ifndef" should the choice and with good compiler support, these are as efficient as the promise made by #pragma once. Plus they are guaranteed to be portable.

2 comments:

CodeWizard said...

I thought I should point out that Microsoft introduced a problem in tmschema.h and schemadef.h in VS2008. They inadvertently included #pragma once at the beginning of these files. It is required to include tmschema.h twice with different #define flags. See Visual Studio 2008 Build Problem and Header File Bugs for more details.

I enjoy your blog very much.

Anonymous said...

Actually, if you read the link you posted you'd notice that while they don't *recommend* using it, it's been removed from the obsolete feature list as of GCC 3.4 (http://gcc.gnu.org/gcc-3.4/changes.html) and is considered to be working properly.