From: "Steve Graegert" <graegerts@gmail.com>
To: Shriramana Sharma <samjnaa@gmail.com>
Cc: Linux C Programming List <linux-c-programming@vger.kernel.org>
Subject: Re: enum types and casting
Date: Fri, 4 May 2007 14:18:35 +0200 [thread overview]
Message-ID: <6a00c8d50705040518o2712d37cw8db39d80a20f3b4e@mail.gmail.com> (raw)
In-Reply-To: <46342FCA.1090400@gmail.com>
On 4/29/07, Shriramana Sharma <samjnaa@gmail.com> wrote:
> Hello. Just now I learnt that enums can be very usefully used as types,
> and was experimenting some with it. The following questions arose.
>
> 1. Are enums allowed as types only in C++ and not in C? gcc rejects
> using an enum as a type whereas g++ accepts it.
As Leslie pointed out, you will have to declare variables of type
enumwith the enum keyword.
> 2.
>
> Consider:
>
> enum BODY { SUN, MOON, STAR } ;
> enum PLANET { EARTH, VENUS, MARS, PLUTO } ;
>
> int main ( void )
> {
> BODY body ;
> // body = 1 ; // gives error
> // body = EARTH ; // gives error
> body = (BODY) 1 ; // does not give error. expected.
> body = (BODY) EARTH ; // does not give error. expected.
> body = 3 ; // does not give error. unexpected.
> body = (BODY) PLUTO ; // does not give error. unexpected.
> }
>
> When the target enum of the cast contains no name that has the same
> integer value as the value being casted, how does g++ accept the cast?
> Is this expected behaviour or a bug?
From the historical perspective, enums are implicitly converted to an
integral value whenever needed. And there __always__ is an integral
value to represent any enum value. That's why assignments of integers
to variables of type enum are perfectly legal. So, for example, when
you compare two enum values, they are both converted to an integer.
However, assignment of an int to an enum variable leads to undefined
behavior if the enum is not large enough to store the integral value.
So, allowing the assignment of any integer, without casting, to a
variable of enum type, would be worse than the opposite.
To convert an integral type to an enumeration, use the following semantic:
enum TEST { zero = 0, one, two };
int i1 = 2;
test t1 = static_cast<TEST> (i1); // t1 == two
The same is true for casts between different enum types.
\Steve
--
Steve Grägert <steve@graegert.com>
Jabber xmpp://graegerts@jabber.org
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2007-05-04 12:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-29 5:40 enum types and casting Shriramana Sharma
2007-05-04 11:26 ` leslie.polzer
2007-05-04 12:18 ` Steve Graegert [this message]
2007-05-16 18:37 ` Shriramana Sharma
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6a00c8d50705040518o2712d37cw8db39d80a20f3b4e@mail.gmail.com \
--to=graegerts@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
--cc=samjnaa@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).