Does #pragma once adequately replace #ifndef #define #endif?

Are there any issues with this?
closed account (z05DSL3A)
#pragma once is an MS thing, if the compiler dose not understand it it will skip it.

I would recommend:
1
2
3
4
5
6
7
8
9
10
#pragma once
#ifndef FOO_H
#define FOO_H
 
struct foo
{
    int member;
};
 
#endif /* FOO_H */ 
Somebody please explain to me why microsoft thought eschewing standard techniques in favor of a compiler-specific
technique was a Good Thing.

In this particular case, it is a Good Thing.

The preprocessor #include directive is a pretty crude tool -- and as Grey Wolf mentioned in the other thread, compile time is significantly impacted when large numbers of files are #included in a project (for large projects, you must also be careful not to exceed the nested #include maximums).

The #pragma once directive is not bullet-proof, but the vast majority of the time it means that the CPP can just stop reading the disk for a given file and get on with its previous task.

My NSH $0.02.


Oh, and FWIW, many compilers support the #pragma once directive, since it is such a good idea. Pedantists don't, but whatever. I always use both.
A #pragma once type feature is discussed in The Evolution and Design of C++, it's not a feature Microsoft introduced. Further more, by the time Microsoft introduced #pragma one, gnu C had tried it and dropped it for reasons that are rare in a Window NT network, but common in a Unix network.

Although Microsoft bashing is fun, it's undeserved here.
Topic archived. No new replies allowed.