Professional C__ - Marc Gregoire [431]
Similarly, if you are writing binary data to a file, you may need to consider what will happen when that file is opened on a system with opposite byte ordering.
Implementation Issues
When a C++ compiler is written, it is designed by a human being who attempts to adhere to the C++ standard. Unfortunately, the C++ standard is more than a thousand pages long and written in a combination of prose, language grammars, and examples. Two human beings implementing a compiler according to such a standard are unlikely to interpret every piece of prescribed information in the exact same way or to catch every single edge case. As a result, compilers will have bugs.
Compiler Quirks and Extensions
There is no simple rule for finding or avoiding compiler bugs. The best you can do is stay up-to-date on compiler updates and perhaps subscribe to a mailing list or newsgroup for your compiler. If you suspect that you have encountered a compiler bug, a simple web search for the error message or condition you have witnessed could uncover a workaround or patch.
One area that compilers are notorious for having trouble with is language additions that were not in the initial standard. For example, some of the template and run-time type features in C++ weren’t originally part of the language, and as a result, some compilers still don’t properly support these features. You will also encounter the same issues with the new features in C++11. Not all compilers support every single new feature yet.
Another issue to be aware of is that compilers often include their own language extensions without making it obvious to the programmer. For example, variable-sized stack-based arrays are not part of the C++ language, yet the following compiles and runs as expected with the g++ compiler:
int i = 4;
char myStackArray[i]; // Not a standard language feature!
Code snippet from VariableArray\VariableArray.cpp
Some compiler extensions may be useful, but if there is a chance that you will switch compilers at some point, you should see if your compiler has a strict mode where it will avoid such extensions. For example, compiling the previous code with the -pedantic flag passed to g++ will yield the following warning:
warning: ISO C++ forbids variable length array 'myStackArray' [-Wvla]
The C++ specification allows for a certain type of compiler-defined language extension through the #pragma mechanism. #pragma is a precompiler directive whose behavior is defined by the implementation. If the implementation does not understand the directive, it ignores it. For example, some compilers allow the programmer to turn compiler warnings off temporarily with #pragma.
Library Implementations
Most likely, your compiler includes an implementation of the C++ Standard Library, including the Standard Template Library. Since the STL is written in C++, however, you aren’t required to use the one that came bundled with your compiler. You could use a third-party STL that, for example, has been optimized for speed, or you could even write your own.
Of course, STL implementers face the same problem that compiler writers face — the standard is subject to interpretation. In addition, certain implementations may make tradeoffs that are incompatible with your needs. For example, one implementation may optimize for speed, while another implementation may focus on using as little memory as possible for containers.
When working with an STL implementation, or indeed any third-party library, it is important to consider the tradeoffs that the designer made during development. Chapter 2 contains a more detailed discussion of the issues involved in using libraries.
Platform-Specific Features
C++ is a great general-purpose language. With the addition of the Standard Library, the language is packed full of so many features that a casual programmer could happily code in C++ for years without going beyond what is built in. However, professional programs require facilities that C++ does not