C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

enums in C and C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Simon Elliott
Guest





PostPosted: Thu Oct 28, 2004 8:40 pm    Post subject: enums in C and C++ Reply with quote



I'm porting thousands of lines of legacy C code to C++. One makor issue
I've got is that of enums being treated differently in C and C++.
There's an automatic cast from int to enum in C but not in C++. The
code is full of constructs like this:

enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;

garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */

Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?

--
Simon Elliott http://www.ctsn.co.uk
Back to top
Victor Bazarov
Guest





PostPosted: Thu Oct 28, 2004 8:45 pm    Post subject: Re: enums in C and C++ Reply with quote



Simon Elliott wrote:
Quote:
I'm porting thousands of lines of legacy C code to C++. One makor issue
I've got is that of enums being treated differently in C and C++.
There's an automatic cast from int to enum in C but not in C++. The
code is full of constructs like this:

enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;

garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */

Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?

The only "clever" thing to do is to keep this C (if it worked before, why
change?)

V

Back to top
Artie Gold
Guest





PostPosted: Thu Oct 28, 2004 8:45 pm    Post subject: Re: enums in C and C++ Reply with quote



Simon Elliott wrote:
Quote:
I'm porting thousands of lines of legacy C code to C++. One makor issue
I've got is that of enums being treated differently in C and C++.
There's an automatic cast from int to enum in C but not in C++. The
code is full of constructs like this:

enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;

garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */

Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?

Why are you bothering to port C code to C++?* Leave it in C and call

what you need (from C++).

HTH,
--ag

* - No slam on C++ intended; just a practical comment.

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."

Back to top
Thierry Miceli
Guest





PostPosted: Thu Oct 28, 2004 9:02 pm    Post subject: Re: enums in C and C++ Reply with quote

Quote:
enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;

garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */

Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?

If you really want to convert the code to C++ you could try to redefine the
'|' operator for the enum. e.g:

enum GardenVegetables { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 };

GardenVegetables operator|(GardenVegetables veg1, GardenVegetables veg2)
{
return (GardenVegetables) ((int)veg1 | (int)veg2);
}

Regards,

Thierry
www.ideat-solutions.com


Back to top
Julián Albo
Guest





PostPosted: Thu Oct 28, 2004 9:12 pm    Post subject: Re: enums in C and C++ Reply with quote

"Simon Elliott" <Simon at ctsn.co.uk> wrote:

Quote:
enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;

garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */

Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?

You can write a garden_veg operator | (garden_veg, garden_veg)

--
Salu2

Back to top
Ioannis Vranos
Guest





PostPosted: Thu Oct 28, 2004 9:30 pm    Post subject: Re: enums in C and C++ Reply with quote

Simon Elliott wrote:

Quote:
I'm porting thousands of lines of legacy C code to C++. One makor issue
I've got is that of enums being treated differently in C and C++.
There's an automatic cast from int to enum in C but not in C++. The
code is full of constructs like this:

enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;

garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */

Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?



Well, a quick fix could be something like:


template <class T>
inline T enum_cast_or(T a, T b) { return static_cast<T>(a|b); }


enum SomeEnum { CARROT=0x01, TURNIP=0x02, PARSNIP=0x04, SPROUT=0x08 };


SomeEnum garden_veg = enum_cast_or(CARROT,PARSNIP);



--
Ioannis Vranos

http://www23.brinkster.com/noicys

Back to top
Jay Nabonne
Guest





PostPosted: Thu Oct 28, 2004 9:33 pm    Post subject: Re: enums in C and C++ Reply with quote

On Thu, 28 Oct 2004 20:40:59 +0000, Simon Elliott wrote:

Quote:
I'm porting thousands of lines of legacy C code to C++. One makor issue
I've got is that of enums being treated differently in C and C++.
There's an automatic cast from int to enum in C but not in C++. The
code is full of constructs like this:

enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;

garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */

Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?

It's not really using it as an enum (once you '|' them, it's not really a
value in the enumeration anymore).

Do:

enum garden_veg_enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 };

int garden_veg;

and let it go... :)

- Jay

Back to top
JKop
Guest





PostPosted: Fri Oct 29, 2004 8:54 am    Post subject: Re: enums in C and C++ Reply with quote

Thierry Miceli posted:

Quote:
enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg;

garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */

Do I really have to trawl through all this code and put in many
hundreds of casts, or is there something clever I can do to get round
this?

If you really want to convert the code to C++ you could try to
redefine
the '|' operator for the enum. e.g:

enum GardenVegetables {
CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 };

GardenVegetables operator|(GardenVegetables veg1, GardenVegetables
veg2) {
return (GardenVegetables) ((int)veg1 | (int)veg2);
}


return static_cast<GardenVegetables>( ( static_cast<int>(veg1) |
static_cast<int>(veg2) ) );


C++, not C/C++ ;-D


-JKop


Back to top
Siemel Naran
Guest





PostPosted: Fri Oct 29, 2004 10:36 am    Post subject: Re: enums in C and C++ Reply with quote

"JKop" <NULL (AT) NULL (DOT) NULL> wrote

Quote:
Thierry Miceli posted:

enum GardenVegetables {
CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 };

GardenVegetables operator|(GardenVegetables veg1, GardenVegetables
veg2) {
return (GardenVegetables) ((int)veg1 | (int)veg2);
}


return static_cast<GardenVegetables>( ( static_cast<int>(veg1) |
static_cast<int>(veg2) ) );


C++, not C/C++ ;-D

It seems fine to use C style casts for converting one integer/float type to
another. Imagine that there exists a constructor
GardenVegetables::GardenVegetables(int). Only for converting one
pointer/reference type to another do I use one of the C++ style casts
(usually static_cast, but sometimes reinterpret_cast).

Also, instead of

Quote:
return (GardenVegetables) ((int)veg1 | (int)veg2);

one could say

return GardenVegetables ( (int(veg1) | int(veg2)) );

but neither way is superior, though I'm not sure if this second way is
allowed in C.


Similarly, "(size_t)i" which is equivalent to "size_t(i)" where i is an int,
also seems fine to me as a good style of C++.



Back to top
JKop
Guest





PostPosted: Fri Oct 29, 2004 10:41 am    Post subject: Re: enums in C and C++ Reply with quote


Quote:
Similarly, "(size_t)i" which is equivalent to "size_t(i)" where i is
an
int, also seems fine to me as a good style of C++.


See but that's why "reinterpret_cast" was given such a horrid name:


float p;

int* k = (int*) &p; //legal

int* k = int*(&p) ; // no can do


To achieve this, you need to be horrid:

int* k = reinterpret_cast<int* const>(&p);


I myself never use (int). Here's how I do it:

Step 1) Use "static_cast". If rejected go to step 2.

Step 2) Use "reinterpret_cast".


I've never had to use "dynamic_cast" so far!


-JKop


-JKop

Back to top
Thierry Miceli
Guest





PostPosted: Fri Oct 29, 2004 3:32 pm    Post subject: Re: enums in C and C++ Reply with quote

Quote:
GardenVegetables operator|(GardenVegetables veg1, GardenVegetables
veg2) {
return (GardenVegetables) ((int)veg1 | (int)veg2);
}


return static_cast<GardenVegetables>( ( static_cast<int>(veg1) |
static_cast<int>(veg2) ) );


C++, not C/C++ ;-D

I agree, I sacrificed purity for readability.


Thierry

Back to top
Siemel Naran
Guest





PostPosted: Sat Oct 30, 2004 10:15 am    Post subject: Re: enums in C and C++ Reply with quote

"JKop" <NULL (AT) NULL (DOT) NULL> wrote


Quote:
Similarly, "(size_t)i" which is equivalent to "size_t(i)" where i is
an
int, also seems fine to me as a good style of C++.


See but that's why "reinterpret_cast" was given such a horrid name:


float p;

int* k = (int*) &p; //legal

int* k = int*(&p) ; // no can do

I agree that for casting between pointer and reference types, it's good to
use static_cast or reinterpret_cast. But for casting between value types, C
style casts which behave like constructors, seem perfectly fine.


Quote:
To achieve this, you need to be horrid:

int* k = reinterpret_cast<int* const>(&p);


I myself never use (int). Here's how I do it:

Step 1) Use "static_cast". If rejected go to step 2.

Step 2) Use "reinterpret_cast".

Agreed for pointer types only.

Quote:
I've never had to use "dynamic_cast" so far!

It arises when writing generic code, and you can't or don't want to edit the
base class by continually adding virtual functions to it.



Back to top
Ioannis Vranos
Guest





PostPosted: Sat Oct 30, 2004 2:06 pm    Post subject: Re: enums in C and C++ Reply with quote

Siemel Naran wrote:

Quote:
I agree that for casting between pointer and reference types, it's good to
use static_cast or reinterpret_cast. But for casting between value types, C
style casts which behave like constructors, seem perfectly fine.


C++ newer casts are still better.




--
Ioannis Vranos

http://www23.brinkster.com/noicys

Back to top
Simon Elliott
Guest





PostPosted: Mon Nov 01, 2004 10:26 am    Post subject: Re: enums in C and C++ Reply with quote

On 28/10/2004, Simon Elliott wrote:
[snip]

Thanks for all the responses to this. I'm still unsure about how to
proceed because there are lots of different enums which are used in
lots of different ways. With the current compiler (BCB3) I have the
(slightly grubby) option of suppressing the warning and leaving
everything as it is. But it's possible that other compilers won't let
me do this.

--
Simon Elliott http://www.ctsn.co.uk
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.