🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #293

Re: std streams and enum?

#293
From: Chris Newton
Date: Mon, 22 Dec 2003 03:02
34 lines
3169 bytes
Maya wrote...
> I am using an enum type that I want to store in a std::IOStream object, 
> but the compiler [complainer] tells me that the format is not supported. 
> enum types are the same as 'const int' right?

No, enumerations are types in their own right. You can implicitly 
convert from an enumerator into its associated numeric value (which will 
have an integral type, though it may or may not be int). However, you 
*cannot* implicitly convert from an integer value to an enumerator, 
which is what your example was effectively trying to do.

Instead, the easiest way to proceed is to read the value into a suitable 
integral type, and then cast to the enumeration type, after performing 
whatever sanity checks are appropriate on the value.

A better but more advanced technique is to provide a specific operator>> 
to read from an istream into the enumeration type, which performs the 
sanity checks itself and then either sets the enumeration value or sets 
appropriate failure flags on the stream. This approach is much easier to 
read and more robust, particularly if you do the smart thing and provide 
a matching operator<< to output the enumeration type to an ostream.

Incidentally, you should be aware that outputting the enumerators as a 
numerical value is a bit of a future-proofing hazard. If you ever 
reorder the enumerators in the definition of the enumeration type, for 
example by inserting a new value somewhere in the middle, your code will 
still compile and run fine, but you'll break backward compatibility with 
any existing data you've written out. For this reason, you might 
consider it safer to have your << and >> operators store the enumerators 
as well-defined strings, independently of their numerical values, rather 
than just casting to/from an integer.

HTH,
Chris
Technical Details
Message-ID: <bs5ms3$43b$1@titan.btinternet.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!feed.news.tiscali.de!news.belwue.de!news.uni-stuttgart.de!carbon.eu.sun.com!btnet-feed5!btnet!news.btopenworld.com!not-for-mail
References: <3FE649C1.70108@netscape.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114592,d75aa99f09707fdd
X-Google-Attributes: gid114592,public
X-Google-Thread: fa69e,d75aa99f09707fdd
X-Google-Attributes: gidfa69e,public
X-Google-ArrivalTime: 2003-12-21 19:03:02 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!feed.news.tiscali.de!news.belwue.de!news.uni-stuttgart.de!carbon.eu.sun.com!btnet-feed5!btnet!news.btopenworld.com!not-for-mail
From: Chris Newton <chrisnewton@no.junk.please.btinternet.com>
Newsgroups: alt.comp.lang.learn.c-c++,borland.public.cpp.borlandcpp
Subject: Re: std streams and enum?
Date: Mon, 22 Dec 2003 03:02:28 +0000 (UTC)
Organization: BT Openworld
Lines: 35
Message-ID: <bs5ms3$43b$1@titan.btinternet.com>
References: <3FE649C1.70108@netscape.net>
NNTP-Posting-Host: host213-122-116-156.in-addr.btopenworld.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: titan.btinternet.com 1072062148 4203 213.122.116.156 (22 Dec 2003 03:02:28 GMT)
X-Complaints-To: news-complaints@lists.btinternet.com
NNTP-Posting-Date: Mon, 22 Dec 2003 03:02:28 +0000 (UTC)
In-Reply-To: <3FE649C1.70108@netscape.net>
X-Accept-Language: en, en-us
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007