C++ Migration Guide | ![]() ![]() ![]() ![]() ![]() |
Migrating From C++ 3.0
This chapter discusses the migration of working code from a C++ 3.0 or 3.0.1 compiler directly to the Sun WorkShop 6 C++ compiler.
5.1 Keywords Added Since the C++ 3.0 Compiler
The following keywords have been added to C++ since the C++ 3.0 compiler. If you use any of these as identifiers, you should change the names. As shown in TABLE 3-1, some keywords can be turned off.
TABLE 5-1 Keywords Added Since C++ 3.0 Compiler bool, false, trueconst_cast, dynamic_cast, reinterpret_cast, static_castexplicitexportmutablenamespace, usingtypenamewchar_t5.2 Source Code Incompatibilities
The following list describes changes that must be made to code that was written for the C++ 3.0 compiler before compiling the code with the Sun WorkShop 6 C++ compiler.
- K&R-style function definitions are no longer allowed. You must use prototype-style definitions.
int f(a) int a; { ... } // Error- You cannot set a global variable
_new_handler
by assignment. Call functionset_new_handler()
instead.- Global
operator
new()
is always used when there is no in-class version. C++ 3.0 incorrectly used an outer-class version in preference to the global version. In the following example, C++ 3.0 would incorrectly useOuter::operator
new
to allocate space.
class Outer {public:void* operator new(size_t);class Inner {... // No operator new};};Outer::Inner* p = new Outer::Inner; // Which operator new?typedef
names cannot be used asstruct
(orclass
orunion
) tags. For example:
typedef struct { int x; } S;struct S b; // Was OK in C++ 3.0, now an errorS c; // Always OK
- Use tags on structures (and classes and unions). The simplest way to fix the earlier example is to use the
typedef
name also as the tag. Such code is allowed in both C and C++:
typedef struct S { int x; } S;struct S b; // Always OKS c; // Always OK- You cannot redefine a name from an outer scope once it has been used in a class. Such redefinition is disallowed by the C++ standard because it can be disastrous, but the C++ 3.0 compiler did not detect the situation. This redefinition is now rejected as an error. For example:
typedef int T;class C {T iv; // type inttypedef float T; // redefine T -- errorT fv; // type float};
- The solution is to change the name of one of the definitions of
T
.- The C++ 3.0 compiler had a bug that allowed a pointer to a function taking unspecified parameters to act in some circumstances as a "universal" pointer-to-function, as in the following example. The C++ rule is that function pointer types must match.
typedef (*pfp)(int,char);typedef (*ufp)(...);int foo(int,char);pfp p = (ufp)foo; // Allowed by C++ 3.0, now an error- Comma-expressions are not allowed in null pointer constants. Although a literal zero is a null pointer constant, an expression such as
(
anything,
0)
is not:
int f();char* g(){return (f(), 0); // OK in 3.0, now an error// should be:// return (f(), (char*)0); // OK// or two statements:// f();// return 0;}- Classes with base classes cannot be initialized with aggregate-initialization syntax. The C++ 3.0 compiler allowed this if no virtual functions were present. You should write a constructor for the class instead.
struct Base { int i; };struct Derived : Base { int j; };Derived d = {1, 2}; // OK with 3.0, now an error
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |