Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ll-xn!cit-vax!news From: news@cit-vax.Caltech.Edu (Usenet netnews) Newsgroups: comp.lang.c++ Subject: Re: Constant expressions Message-ID: <1288@cit-vax.Caltech.Edu> Date: Fri, 5-Dec-86 18:46:59 EST Article-I.D.: cit-vax.1288 Posted: Fri Dec 5 18:46:59 1986 Date-Received: Fri, 5-Dec-86 22:48:53 EST References: <1252@cit-vax.Caltech.Edu> <6426@alice.uUCp> Reply-To: jon@cit-vax.UUCP (Jon Leech) Organization: California Institute of Technology Lines: 60 Organization : California Institute of Technology Keywords: From: jon@oddhack.Caltech.Edu (Jon Leech) Path: oddhack!jon In article <6426@alice.uUCp> shopiro@alice.UUCP writes: >In article <1252@cit-vax.Caltech.Edu>, jon@oddhack.Caltech.Edu (Jon Leech) writes: >> >> I recently found that C++ (1.1) does not allow integral casts in >> constant expressions in some cases; for example, >> >> typedef enum { one = (unsigned)1 } ENUM; >> >> Unfortunately, this makes it impossible to include some of the graphics header >> files on Suns which use this form (it also happens to disagree with >> the ANSI C draft, sadly). Has anyone got a fix for this problem? > >This code (or any code that uses casts in constant expressions) is not >legal C according to Ritchie* or the ANSI draft. Apparently some C >compilers will accept it; I believe there are others that won't. >C++ has already abandoned complete upward compatability with legal >C -- it's unreasonable to expect it to be compatible with illegal C. > >I grant that it is clear what such casts would mean if they were >legal, and that there are examples where they might allow you to >write code that couldn't be written as directly otherwise. I am >confident that if this extension were approved by the ANSI committee >it would also appear in C++. > >However, in all the SUN header examples I have looked at, the cast is >pure noise and can be eliminated with no effect whatsoever. That is >what I suggest you do. (When you're done, I'm sure there will be many >people who would like a copy). > >* Dennnis Ritchie, The C Programming Language - Reference Manual > Sep. 1980. In my April 30, 1985 copy of the draft, Section C.4 ``Constant Expressions'' says `` For constant expressions in initializers, in addition to integral constant expressions as discussed previously, one can also use floating constants and arbitrary casts...'' This is not allowed in K&R unless I'm missing something (Appendix A, Section 15, pg. 211). Unless this has been changed in later drafts, we are both partially right; you're right that the particular code I quote above is illegal, and I'm right that the usage is in fact acceptable - in initializers. What a stupid restriction. Time to comment to ANSI. It turns out to be far easier to write C hooks to the Sun graphics routines. Annoying, but less pain than changing header files. -- Jon Leech (jon@csvax.caltech.edu || ...seismo!cit-vax!jon) Caltech Computer Science Graphics Group __@/