|C H A P T E R 2|
Using Compatibility Mode
This chapter describes how to compile code that was intended for the C++ 4 compilers.
The compiler options for compatibility mode are (both versions mean the same thing):
There are some minor differences between using the C++ 4 compilers and the C++ 5 compilers in compatibility mode, as described in the following sections.
By default, some of the new C++ keywords are recognized as keywords in compatibility mode, but you can turn off most of these keywords with compiler options, as shown in the following table. Changing the source code to avoid the keywords is preferable to using the compiler options.
Keyword typename cannot be disabled. The additional new C++ keywords, described in TABLE 3-1, are disabled by default in compatibility mode.
The C++ 5 compilers do a better job of enforcing some C++ language rules. They are also less permissive about anachronisms.
If you compile with C++ 4 and enable anachronism warnings, you might discover code that has always been invalid, but that much older C++ compilers accepted anyway. It was always explicit policy (that is, stated in the manuals) that the anachronisms would cease to be supported in future compiler releases. The anachronisms consist mainly of violating access (private, protected) rules, violating type-matching rules, and using compiler-generated temporary variables as the target of reference parameters.
The remainder of this section discusses the rules that previously were not enforced, but are now enforced by the C++ compiler.
When initializing an object, or passing or returning a value of class type, the copy constructor must be accessible.
Solution: Make the copy constructor accessible. Usually, it is given public access.
The static storage class applies to objects and functions, not to types.
Solution: In this example, the static keyword does not have any meaning for class C and should be removed.
When allocating an object with new, the matching operator delete must be accessible.
Solution: Make the delete operator accessible. Usually, it is given public access.
A count is not allowed in a delete expression.
If you allocate a const object with new, it must be initialized.
The C++ standard introduced a change in the rules for conditional expressions. The C++ compiler uses the new rule in both standard mode and compatibility mode. For more information, see Section 1.5, Conditional Expressions.
Default parameter values on overloaded operators or on pointers to functions are not allowed.
Solution: You must write the code some other way, probably by providing additional function or function pointer declarations.
Trailing commas in function argument lists are not allowed.
Solution: Remove the extra comma.
Passing a const or literal value to a nonconstant reference parameter is not allowed.
Solution: If the function does not modify its parameter, change the declaration to take a const reference (for this example, const T&). If the function modifies the parameter, you cannot pass it a const or a literal value. An alternative is to create an explicit nonconstant temporary and pass that instead. See Section 3.7, String Literals and char* for related information.
The C++ compiler, in both compatibility and standard mode, now issues a warning for implicit and explicit conversions between pointer-to-function and void*. For more information, see Section 1.6, Function Pointers and void*.
If an object of enum type is assigned a value, that value must have the same enum type.
Solution: Use a cast.
The old C++ syntax of implied base-class name in a member-initializer list is not allowed.
const and volatile qualifiers on pointers must match properly when passing arguments to functions, and when initializing variables.
Solution: If the function does not modify the characters it points to, declare the parameter to be const char*. Otherwise, make a nonconstant copy of the string and pass that instead.
Nested types cannot be accessed from outside the enclosing class without a class qualifier.
In class template definitions and declarations, appending the type argument bracketed by < > to the class's name has never been valid, but versions 4 and 5.0 of the C++ compiler did not report the error. For example, in the following code the <T> appended to MyClass is invalid for both the definition and the declaration.
Solution: Remove the bracketed type argument from the class name, as shown in the following code.
The template compilation model for compatibility mode is different from the 4.2 compilation model. For more information about the new model, refer to Section 3.3.5, Template Repository.