Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: errata in C++ Primer Message-ID: <9510@alice.UUCP> Date: 20 Jun 89 12:55:52 GMT References: <883@kaiser.UUCP> <1304@garcon.cso.uiuc.edu> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 87 In article <1304@garcon.cso.uiuc.edu>, mcdaniel@uicsrd.csrd.uiuc.edu (Tim McDaniel) writes: > I'd like to see more details on what the semantics exactly are. The > only examples here are for assignment, but for full clarity, I need to > know the results of applying any operator to an enum operand. Every enumerated type is a separate type. When used in a context that requires an integer, a value of an enumerated type is quietly converted to an int. Converting the other way requires a cast. > What happens when I have values and operations like: > enum_X enum_X => enum_X ? If is something like `+', the result is an int. > Y* enum_X => Y* or error? The enum_X value is quietly converted to int; the result is Y*. > int enum_X => enum_X or error? The result is an int. > type var[enum_X]; => tye var[(int) enum_X]; or error? The enum_X is converted to an int. > Suppose I want to count the number of times NOT_RUN, FAIL, and PASS > occur. So I'd actually do something like this: > enum TestStatus { NOT_RUN, FAIL, PASS, MAX_TEST }; > int status_count[MAX_TEST]; OK so far. > for (TestStatus i = NOT_RUN; i < MAX_TEST; i++) > status_count[i] = 0; > > enum TestStatus rc; > while (dotest(&rc)) > status_count[rc]++; It's legal. The only part of this that's questionable is the notion of applying ++ to an enum, but I'm pretty sure that's OK even though the corresponding use of + would require a cast to convert the result back to enum. > Bleah. Are there any other cases in C++ where > type var = expr; > is not equivalent to > type var; > var = expr; > ? Are there any other possible "implicit conversions"? Saying type var = expr; is NEVER equivalent to saying type var; var = expr; The first case creates var and simultaneously initializes it to expr. The second creates var, initializes it to a default value (which is often garbage) and then obliterates that value with `expr.' It is possible to define types for which either of these is legal and the other is illegal. > >p.40 : named enumerations now define unique integral types > What's a "named enumeration"? Is > typedef enum {FOO, BAR} widget; > a named enumeration? What if we have > enum {FOO, BAR} a; > a = 1; // legal or no? No -- you need a cast to convert an integer to an enumerated type. > Why are "named" enumerations distinguished from "unnamed" ones? An unnamed enumeration doesn't give you a useful handle for the type it defines. It defines several objects (the one above defines FOO, BAR, and a) of a nameless type. Since the type is nameless, it's not possible to get at it again. -- --Andrew Koenig ark@europa.att.com