Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!pp!riunite!rfg From: rfg@riunite.ACA.MCC.COM (Ron Guilmette) Newsgroups: comp.lang.c++ Subject: Re: Enumerated types in C++ Message-ID: <176@riunite.ACA.MCC.COM> Date: 15 Apr 89 18:40:27 GMT References: <874@sl10c.concurrent.co.uk> Reply-To: rfg@riunite.UUCP (Ron Guilmette) Organization: MCC Austin, Texas Lines: 77 In article <874@sl10c.concurrent.co.uk> Andy Chittenden writes: >Enumerated types in C++ are not strictly typed like they are in other >languages. A statement of the form: > > enum { FALSE = (0 != 0), TRUE = (0 == 0) } bool; > >does not prevent values other than TRUE or FALSE to be assigned to the >variable bool. Since someone else has opened up the topic of enum types and the degree to which they are "strictly typed", I would like to pose another "language lawyer" type question. I have just learned that the following pair of declarations, if given within the same scope, is illegal (at least GNU G++ thinks so): enum color { red, orange, yellow }; enum fruit { banana, pear, orange }; A look at *The Book* tells why. Essentially, the above declarations are considered to be shorthand for: typedef int color; const int red = 0; const int orange = 1; const int yellow = 2; typedef int fruit; const int banana = 0; const int pear = 1; const int orange = 2; // ERROR - redeclared Thus, there is an illegal redeclaration of orange within the same scope. Now consider the following which G++ *does* allow: ----------------------------------------------------------------------------- enum color { red, green, blue }; enum fruit { apple, banana }; overload function; void function (color c) { } void function (fruit f) { } void function (int i) { } int test () { function (apple); function (red); function (99); } ----------------------------------------------------------------------------- For the three function calls the compiler performs a disambiguation process for the overloaded function which takes into account the specific enum type of each constant argument. Yet if we go strictly by the "shorthand" rule, in which enum type declarations are just shorthand for a series of const int declarations, then this disambiguation should never take place, and we should get errors for effectively providing three different definitions for the "void function (int)" function. Is g++ handling the above source code incorrectly, or does a "standard conforming" C++ compiler/translator have to remember the precise (enum) type associated with any given enum constant name? If it does have to remember, then does it also have to get temporary amnesia in most cases? I guess that the bottom line question is "What is the TYPE of an enum constant?" -- // Ron Guilmette - MCC - Experimental Systems Kit Project // 3500 West Balcones Center Drive, Austin, TX 78759 - (512)338-3740 // ARPA: rfg@mcc.com // UUCP: {rutgers,uunet,gatech,ames,pyramid}!cs.utexas.edu!pp!rfg