Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!tcs!nujoizey!gwu From: gwu@nujoizey.tcs.com (George Wu) Newsgroups: comp.lang.c++ Subject: Re: porting tips for c++ Message-ID: <943@tcs.tcs.com> Date: 8 Aug 90 00:32:36 GMT References: <1990Aug6.133901.25745@swbatl.sbc.com> Sender: usenet@tcs.com Reply-To: gwu@nujoizey.tcs.com (George Wu) Organization: Teknekron Communications Systems Lines: 118 Here's something I wrote up for our people when we switched from GNU C++ 1.36.2 to Sun C++ (AT&T Cfront 2.0). George MEMORANDUM FROM: George Wu TO: User Interface Team DATE: June 27, 1990 SUBJECT: Switching from GNU C++ to Sun C++ _______________________________________________________________________________ This memorandum summarizes the necessary procedures used to switch from GNU's C++ compiler to Sun's C++ compiler (AT&T Cfront 2.0). Two areas are addressed: general code changes; and environmental changes specific to the IMS project. There are several attachments to this memorandum: a description of CC and various C++ tools; a list of compiler implementation specific behavior; and a list compiler "not implemented" error messages with translations to English (known to be incomplete). GENERAL C++ CODE MODIFICATIONS ============================== The following discoveries were made in porting IMS UI code the Sun C++: o Constructors cannot have default argument values. The compiler does not always complain about this problem. Consider the following example: class C { public: C(int x = 0); C(int x, int y = 0); }; In order to compile this code with Sun's C++ compiler, the following re-arrangement may be necessary: class C { public: C(int x); C(int y, int y); private: init(int x = 0, int y = 0); }; C::C(int x) { init(x); } C::C(int x, int y) { init(x, y); } o Inline functions cannot contain loops. The fix is obvious: make the function an ordinary one. o Default argument values can only be declared in one place. Declare default argument values in the class definition, not in the function definition. o Values of type void* can only be assigned to variables of type void*. The C++ permits any pointer value to be automatically cast to a void* where necessary, but this relationship is not symmetrical. Expressions of type void* must therefore be explicitly cast to the appropriate pointer type. For example, given char* s and void* p, s = p becomes s = (char *) p. o The name of a function is not automatically converted to the address of the function when assigned to a function pointer. Explicitly use the address of operator (&). The Sun C++ compiler appears to only complain about this under certain, indeterminate conditions. When it doesn't complain, the Sun C++ compiler sometimes generates bad code. o The type of a pointer to a function includes the function argument types, ie. the function prototyping must also be specified when using function pointer. o When a pointer to a class member function is used, explicitly resolve the class scope. Even in cases where there is no possibilty of confusion, C++ requires the scope. Therefore, instead of: fp = &init; you must state: fp = &C::init; Failure to specify the class will result in the C++ compiler generating bad code, as evidenced by a core dump when the function pointer is dereferenced. o Default argument values which require run-time calculations may cause the C compiler (to which Cfront outputs C code) to complain about undefined temporary variables. For instance: class C { public: C(int size = round(0.5 * inches)); }; will cause the C compiler to generate an error message complaining about an undefined variable. The variable name will not only be mangled, but will fail to unmangle to any C++ variable. ---- George J Wu | gwu@tcs.com or ucbcad!tcs!gwu Software Engineer | 2121 Allston Way, Berkeley, CA, 94704 Teknekron Communications Systems, Inc.| (415) 649-3752