🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++.moderated
Article #51

Re: enum expression is IF statement

#51
From: Greg Sternberg/PDS Inc
Date: Fri, 16 Feb 1996 11:25
63 lines
2861 bytes
Peter van Klaveren (P.v.Klaveren@bcs.cs.philips.com) wrote:
: enum
: {
:    CSEQ_IDLE,
:    CSEQ_ERROR,
:    CSEQ_ABORT,
:    CSEQ_WAIT4RESPONSE
: };

: int state = 3;

: if (state == CSEQ_WAIT4RESPONSE)
: {
:     // this code is not executed!!!
:     ...
: }

   You're making the assumption that the compiler is numbering your enums
   sequentially from 0 - since the standard says nothing about how the
   compiler could number your enums it could very well be using 0x1, 0x2, 
   0x4, and 0x8.

: BOOL expr_is_valid = state == CSEQ_WAIT4RESPONSE;

: if (expr_is_valid)
: {
:     // this code IS executed!!!
:     ...
: }

: I know it is not recommended to compare integers with enumeration
: values, but why on earth is 'expr_is_valid' TRUE but seems
: 'state == CSEQ_WAIT4RESPONSE' to result in FALSE (at least in a if
: expression calcuation)?

   If I remember the leeway in the standard about enums was allowed so 
   the compiler could optimize enums in any fashion it choose (i.e. bit
   fields or char vs int)
{ If this is true this is a fairly obscure, important C incompatibility
  references anyone ? -mod}

: Anybody an idea?

: Peter.
:                                              \\\//
:                                             -(o o)-
: ==========================================ooO-(_)-Ooo======
: Peter van Klaveren, E-mail: P.v.Klaveren@bcs.cs.philips.com
: ===========================================================

:       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
:       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
:       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
:       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]

--
CompuServ: 75511,65          "I don't know - I'm making this up as I go along." 
Internet: gstern@usa.net     Indiana Jones - Raiders of the Lost Ark

      [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
      [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
      [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
      [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
Technical Details
Message-ID: <4g1pj9$pqg@netlab.cs.rpi.edu>
Path: arc1-ca.pugleaf.net!newsstand.cit.cornell.edu!ub!newserve!rebecca!rpi!not-for-mail
References: <4fq7dc$pac@netlab.cs.rpi.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newsstand.cit.cornell.edu!ub!newserve!rebecca!rpi!not-for-mail
From: gstern@earth.usa.net (Greg Sternberg/PDS Inc)
Newsgroups: comp.lang.c++.moderated,comp.lang.c++
Subject: Re: enum expression is IF statement
Followup-To: comp.lang.c++.moderated,comp.lang.c++
Date: 16 Feb 1996 11:25:29 -0000
Organization: Internet Express (800-592-1240 customer service)
Sender: cppmods@netlab.cs.rpi.edu
Approved: devitto@ferndown.ate.slb.com
Message-ID: <4g1pj9$pqg@netlab.cs.rpi.edu>
References: <4fq7dc$pac@netlab.cs.rpi.edu>
Reply-To: news@shiva.usa.net
NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: 16 Feb 1996 06:30:31 GMT