* enum types and casting
@ 2007-04-29 5:40 Shriramana Sharma
2007-05-04 11:26 ` leslie.polzer
2007-05-04 12:18 ` Steve Graegert
0 siblings, 2 replies; 4+ messages in thread
From: Shriramana Sharma @ 2007-04-29 5:40 UTC (permalink / raw)
To: Linux C Programming List
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.
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?
Thanks as always.
Shriramana Sharma.
P.S: Oops, silly me, Pluto isn't a planet any more...
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: enum types and casting
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
1 sibling, 0 replies; 4+ messages in thread
From: leslie.polzer @ 2007-05-04 11:26 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
[-- Attachment #1: Type: text/plain, Size: 748 bytes --]
On Sun, Apr 29, 2007 at 11:10:26AM +0530, Shriramana Sharma wrote:
> 1. Are enums allowed as types only in C++ and not in C?
They are part of ANSI C.
> gcc rejects using an enum as a type whereas g++ accepts it.
Not really. You need to say "enum BODY body".
> 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?
You are doing C style casts here, which you shouldn't.
The compiler will warn you at best, but in the end he will handle
everything as raw bytes.
Leslie
--
Personal homepage: https://viridian.dnsalias.net/~sky/homepage/
gpg --keyserver pgp.mit.edu --recv-keys DD4EBF83
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: enum types and casting
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
2007-05-16 18:37 ` Shriramana Sharma
1 sibling, 1 reply; 4+ messages in thread
From: Steve Graegert @ 2007-05-04 12:18 UTC (permalink / raw)
To: Shriramana Sharma; +Cc: Linux C Programming List
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
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: enum types and casting
2007-05-04 12:18 ` Steve Graegert
@ 2007-05-16 18:37 ` Shriramana Sharma
0 siblings, 0 replies; 4+ messages in thread
From: Shriramana Sharma @ 2007-05-16 18:37 UTC (permalink / raw)
To: Steve Graegert; +Cc: Linux C Programming List
Steve Graegert wrote:
> 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
Steve,
I tried this as follows:
enum BODY { SUN, MOON, STAR } ;
enum PLANET { EARTH, VENUS, MARS, PLUTO } ;
int main ( void )
{
BODY body ;
body = static_cast < BODY > ( 3 ) ; // does not give error.
body = static_cast < BODY > ( PLUTO ) ; // does not give error.
}
but it still does not throw an error even though the enum being casted
*to* does not contain an identifier equivalent to the value actually
being casted, whether a primitive integer or an identifier belonging to
another enum.
Don't you think this is a GCC bug?
Shriramana.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-05-16 18:37 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2007-05-16 18:37 ` Shriramana Sharma
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).