C++ Migration Guide | ![]() ![]() ![]() ![]() ![]() |
Using Compatibility Mode
This chapter describes how to compile code that was intended for the C++ 4 compilers.
2.1 Compatibility Mode
The compiler options for compatibility mode are (both versions mean the same thing):
-compat-compat=4For example:
example%CC -compat -O myfile.cc mylib.a -o myprog
There are some minor differences between using the C++ 4 compiler and the Sun WorkShop 6 C++ compiler in compatibility mode, as described in the following sections.
2.2 Keywords in Compatibility Mode
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.
TABLE 2-1 Keywords in Compatibility Mode explicit
-features=no%explicit
export
-features=no%export
mutable
-features=no%mutable
typename
cannot disable
Keyword
typename
cannot be disabled. The additional new C++ keywords, described in TABLE 3-1, are disabled by default in compatibility mode.2.3 Language Semantics
The Sun WorkShop 6 C++ compiler does a better job of enforcing some C++ language rules. It is 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 rules that previously were not enforced are as follows:
2.3.1 Copy Constructor
When initializing an object, or passing or returning a value of class type, the copy constructor must be accessible.
class T {T(const T&); // private};void f1(T t) { } // Error, can't pass a TT f2() { T t; return t; } // Error, can't return a T
- Solution: Make the copy constructor accessible. Usually, it is given public access.
2.3.2 Static Storage Class
The
static
storage class applies to objects and functions, not to types.
static class C {...}; // Error, cannot use static herestatic class D {...} d; // OK, d is static
- Solution: In this example, the
static
keyword does not have any meaning for classC
and should be removed.2.3.3 Operators
new
anddelete
When allocating an object with
new
, the matchingoperator
delete
must be accessible.
class T {void operator delete(void*); // privatepublic:void* operator new(size_t);};T* t = new T; // Error, operator delete is not accessible
- Solution: Make the
delete
operator accessible. Usually, it is given public access.A version of
operator
new
oroperator
delete
that is not a class member must not be declared static.
static void* operator new(size_t); // Error
- Solution: Make the function global.
A count is not allowed in a
delete
expression.
delete [5] p; // Error: should be delete [] p;2.3.4
new
const
If you allocate a
const
object withnew
, it must be initialized.
const int* ip1 = new const int; // Errorconst int* ip2 = new const int(3); // OK2.3.5 Conditional Expression
The C++ standard introduced a change in the rules for conditional expressions. The Sun WorkShop 6 C++ compiler uses the new rule in both standard mode and compatibility mode. For more information, see Section 1.4 Conditional Expressions.
2.3.6 Default Parameter Value
Default parameter values on overloaded operators or on pointers to functions are not allowed.
T operator+(T t1, T t2 = T(0) ); // Errorvoid (*fptr)(int = 3); // Error
- Solution: You must write the code some other way, probably by providing additional function or function pointer declarations.
2.3.7
main()
Return TypeThe function
main
must have a return type ofint
.
void main(){ ... } // Error
- Solution: Give
main
a return type ofint
. You do not need to add a return statement. If a return statement is present, it must return anint
value.2.3.8 Trailing Commas
Trailing commas in function argument lists are not allowed.
f(int i, int j, ){ ... } // Error
- Solution: Remove the extra comma.
2.3.9 Passing of
const
and Literal ValuesPassing a
const
or literal value to a nonconstant reference parameter is not allowed.
void f(T&);extern const T t;void g() {f(t); // Error}
- 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 aconst
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.2.3.10 Conversion Between Pointer-to-Function and
void*
The C++ compiler, in both compatibility and standard mode, now issues a warning for implicit and explicit conversions between pointer-to-function and
void*
when the+w2
option is passed. In both modes the compiler no longer recognizes such implicit conversions when resolving overloaded function calls. For more information, see Section 1.5 Function Pointers and void*.2.3.11 Type
enum
If an object of
enum
type is initialized or assigned a value, that value must have the sameenum
type.
enum E { one = 1 };E e1 = 1; // Error, can not use an int to intialize an EE e2 = E(1); // OK with cast
- Solution: Use a cast.
2.3.12 Macro Redefinition
Macros cannot be redefined to a different value without an intervening
#undef
.
#define count 1#define count 2 // Error
- Solution: Remove one of the
#define
statements, or put a#undef
count
statement before the second#defin
e.2.3.13 Member-Initializer List
The old C++ syntax of implied base-class name in a member-initializer list is not allowed.
struct B { B(int); };struct D : B {D(int i) : (i) { } // Error, should be B(i)};2.3.14
const
andvolatile
Qualifiers
const
andvolatile
qualifiers on pointers must match properly when passing arguments to functions, and when initializing variables.
void f(char *);const char* p = "hello";f(p); // Error: passing const char* to non-const char*
- 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.2.3.15 Nested Type
Nested types cannot be accessed from outside the enclosing class without a class qualifier.
struct Outer {struct Inner { int i; };int j;};Inner x; // Error; should be Outer::Inner
2.3.16 Class Template Definitions and Declarations
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 toMyClass
is invalid for both the definition and the declaration.
template<class T> class MyClass<T> { ... }; // definitiontemplate<class T> class MyClass<T>; // declarationSolution: Remove the bracketed type argument from the class name, as shown in the following code.
template<class T> class MyClass { ... }; // definitiontemplate<class T> class MyClass; // declaration2.4 Template Compilation Model
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.
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |