While not part of the standard yet, the widespread use of #pragma once is likely to find more support down the road. The big advantage is the simplicity for novice developers, no worrying about problems. The old #ifndef approach has a disadvantage of cluttering the global namespace as well. We recommend for new code that #pragma once is acceptable as most vendors now support it.
// check for C++ in use #if !defined(__cplusplus) #error C++ compiler required. #endif // common headers #pragma once #inlcude <iostream> #inlcude <fstream> #inlcude <vector> #inlcude <string> #inlcude <algorithm>
Now with some headers compartmentalized, it easy to see how this example can be used for many simple projects. The check is to be sure the compiler is set for C++ as the headers are different for the C language. C++ is much more sophisticated and it’s now one of the most popular programming languages used,
The example shows some basic C++ headers that are so commonly used they should be bundled in the main header so that every component can use them.
C++ uses templates for many functions. This allows the language to be less type specific. A good example is the <vector> template and user defined containers. By providing support for operators such as comparisons, the user defined type can be sorted etc.
Many classes are defined as templates so that they can be more generalized. C++ supports bits, bytes and larger containers so depending on the requirements the idea of templates makes a lot of sense. So @pragma once means that template developers can easily prepare their library without worry.
The use of C++ namespaces are best used in the main code files. It’s not suitable to use namespaces in a header in most cases. A class template may be contained in a namespace in certain circumstances.
Microsoft uses the old style #ifndef type guards for the Win32 API. The Microsoft foundation classes also use the traditional guards. This is fully compatible with the #pragma once for new code. Microsoft uses C for some of the lowest levels of the system.