Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #13679 > unrolled thread

one enum is a subset of another

Started byChristopher <cpisz@austin.rr.com>
First post2012-01-31 14:56 -0800
Last post2012-02-01 12:28 -0600
Articles 11 — 8 participants

Back to article view | Back to comp.lang.c++


Contents

  one enum is a subset of another Christopher <cpisz@austin.rr.com> - 2012-01-31 14:56 -0800
    Re: one enum is a subset of another Paul N <gw7rib@aol.com> - 2012-01-31 15:19 -0800
      Re: one enum is a subset of another Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-31 23:56 +0000
    Re: one enum is a subset of another Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-02-01 09:51 +0100
      Re: one enum is a subset of another Larry Evans <cppljevans@suddenlink.net> - 2012-02-01 11:46 -0600
        Re: one enum is a subset of another Marcel Müller <news.5.maazl@spamgourmet.com> - 2012-02-01 21:14 +0100
    Re: one enum is a subset of another Thomas Boell <tboell@domain.invalid> - 2012-02-01 14:08 +0100
      Re: one enum is a subset of another Tobias Müller <troplin@bluewin.ch> - 2012-02-01 21:34 +0000
      Re: one enum is a subset of another "Matt D." <nospam@invalid.local> - 2012-02-02 00:37 +0100
        Re: one enum is a subset of another Thomas Boell <tboell@domain.invalid> - 2012-02-02 02:38 +0100
    Re: one enum is a subset of another Larry Evans <cppljevans@suddenlink.net> - 2012-02-01 12:28 -0600

#13679 — one enum is a subset of another

FromChristopher <cpisz@austin.rr.com>
Date2012-01-31 14:56 -0800
Subjectone enum is a subset of another
Message-ID<7aac0ea8-61ab-481e-80a6-c9ab215b009e@gi10g2000vbb.googlegroups.com>
A developer before me created a large enumeration we can call
EnumType1.
He then, right under it, typedef-ed another

typedef EnumType1 EnumType2

He then created a comment "alias, should only include x types", where
x is a rule defining a subset of EnumType1

This is crap imo. Any function or method declared to take EnumType2,
would happily take a value from  EnumType1 that does not meet the x
criteria.

Since I know the subset from EmunType1 that meets the x criteria, how
can I define a subset EnumType2 that only includes those enums without
typing the whole darn subset twice?

Example
enum Fruit
{
   APPLE = 0,
   BANANA,
   ORANGE,
   TANGERINE,
   GRAPEFRUIT,
   NUM_FRUITS
};

typedef Fruit Citrus;   // This is crap and I want to fix it


[toc] | [next] | [standalone]


#13680

FromPaul N <gw7rib@aol.com>
Date2012-01-31 15:19 -0800
Message-ID<49471cce-a14e-4e1d-a6c6-2b5aacf6f56c@dn8g2000vbb.googlegroups.com>
In reply to#13679
On Jan 31, 10:56 pm, Christopher <cp...@austin.rr.com> wrote:
> A developer before me created a large enumeration we can call
> EnumType1.
> He then, right under it, typedef-ed another
>
> typedef EnumType1 EnumType2
>
> He then created a comment "alias, should only include x types", where
> x is a rule defining a subset of EnumType1
>
> This is crap imo. Any function or method declared to take EnumType2,
> would happily take a value from  EnumType1 that does not meet the x
> criteria.
>
> Since I know the subset from EmunType1 that meets the x criteria, how
> can I define a subset EnumType2 that only includes those enums without
> typing the whole darn subset twice?
>
> Example
> enum Fruit
> {
>    APPLE = 0,
>    BANANA,
>    ORANGE,
>    TANGERINE,
>    GRAPEFRUIT,
>    NUM_FRUITS
>
> };
>
> typedef Fruit Citrus;   // This is crap and I want to fix it

I'm not sure you can do what you want. In view of which, what your
developer has done doesn't seem entirely stupid - the compiler won't
catch any errors but things are more obvious to a human reader. For
instance:

typedef Fruit Apple_or_Banana;

Apple_or_Banana x;

x = ORANGE;

 - the compiler won't spot the error, but you can.

[toc] | [prev] | [next] | [standalone]


#13681

FromJorgen Grahn <grahn+nntp@snipabacken.se>
Date2012-01-31 23:56 +0000
Message-ID<slrnjigvt3.1ls.grahn+nntp@frailea.sa.invalid>
In reply to#13680
On Tue, 2012-01-31, Paul N wrote:
> On Jan 31, 10:56 pm, Christopher <cp...@austin.rr.com> wrote:
>> A developer before me created a large enumeration we can call
>> EnumType1.
>> He then, right under it, typedef-ed another
>>
>> typedef EnumType1 EnumType2
>>
>> He then created a comment "alias, should only include x types", where
>> x is a rule defining a subset of EnumType1
>>
>> This is crap imo. Any function or method declared to take EnumType2,
>> would happily take a value from  EnumType1 that does not meet the x
>> criteria.
>>
>> Since I know the subset from EmunType1 that meets the x criteria, how
>> can I define a subset EnumType2 that only includes those enums without
>> typing the whole darn subset twice?
>>
>> Example
>> enum Fruit
>> {
>>    APPLE = 0,
>>    BANANA,
>>    ORANGE,
>>    TANGERINE,
>>    GRAPEFRUIT,
>>    NUM_FRUITS
>>
>> };
>>
>> typedef Fruit Citrus;   // This is crap and I want to fix it
>
> I'm not sure you can do what you want. In view of which, what your
> developer has done doesn't seem entirely stupid - the compiler won't
> catch any errors but things are more obvious to a human reader.

Depends on your attitude to typedefs ... I am suspicious them (in this
usage) because they introduce uncertainty into an area where usually
the compiler guarantees correctness (i.e. if Fruit and Citrus had been
classes).

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .

[toc] | [prev] | [next] | [standalone]


#13686

FromMarcel Müller <news.5.maazl@spamgourmet.com>
Date2012-02-01 09:51 +0100
Message-ID<4f28fcff$0$6566$9b4e6d93@newsspool3.arcor-online.net>
In reply to#13679
On 31.01.2012 23:56, Christopher wrote:
> A developer before me created a large enumeration we can call
> EnumType1.
> He then, right under it, typedef-ed another
>
> typedef EnumType1 EnumType2
>
> He then created a comment "alias, should only include x types", where
> x is a rule defining a subset of EnumType1
>
> This is crap imo. Any function or method declared to take EnumType2,
> would happily take a value from  EnumType1 that does not meet the x
> criteria.

Exactly. You need different types.

> Since I know the subset from EmunType1 that meets the x criteria, how
> can I define a subset EnumType2 that only includes those enums without
> typing the whole darn subset twice?

There is no language support for this kind of problem.

AFAIK you have two options:
1. repeat the definition of the common constants.
2. declare your own enum classes.

In the latter case you need to know that Citrus is no subclass of Fruit, 
because you can safely cast from the subset to the general one but not 
the other way around.

class Citrus
{public:
   static const Citrus ORANGE;
   static const Citrus GRAPEFRUIT;
   //...

  protected:
   Citrus() {}
};

class Fruit : Citrus
{ static const Fruit APPLE;
   //...
  protected:
   Fruit() {}
};

bool operator==(const Citrus& l, const Citrus& r)
{ return &l == &r;
}
bool operator!=(const Citrus& l, const Citrus& r)
{ return &l != &r;
}

Note that this pseudo enums have no value, since the fixed number of 
instances is already sufficient for uniqueness. This is similar to Java 
2 like enums.

Things get complicated when you need to define different subsets. 
Especially the comparison operators have to be overloaded appropriately. 
And if you also require an associated int value, you need a virtual base 
class holding that value or alternatively build your own lookup table to 
ensure uniqueness over the related class types.


Marcel

[toc] | [prev] | [next] | [standalone]


#13693

FromLarry Evans <cppljevans@suddenlink.net>
Date2012-02-01 11:46 -0600
Message-ID<4f297a5c$0$19757$bbae4d71@news.suddenlink.net>
In reply to#13686

[Multipart message — attachments visible in raw view] — view raw

On 02/01/12 02:51, Marcel Müller wrote:
> On 31.01.2012 23:56, Christopher wrote:
>> A developer before me created a large enumeration we can call
>> EnumType1.
>> He then, right under it, typedef-ed another
>>
>> typedef EnumType1 EnumType2
>>
>> He then created a comment "alias, should only include x types", where
>> x is a rule defining a subset of EnumType1
>>
>> This is crap imo. Any function or method declared to take EnumType2,
>> would happily take a value from  EnumType1 that does not meet the x
>> criteria.
> 
> Exactly. You need different types.
> 
>> Since I know the subset from EmunType1 that meets the x criteria, how
>> can I define a subset EnumType2 that only includes those enums without
>> typing the whole darn subset twice?
> 
> There is no language support for this kind of problem.
> 
> AFAIK you have two options:
> 1. repeat the definition of the common constants.
> 2. declare your own enum classes.
> 
> In the latter case you need to know that Citrus is no subclass of Fruit,
> because you can safely cast from the subset to the general one but not
> the other way around.
> 
> class Citrus
> {public:
>   static const Citrus ORANGE;
>   static const Citrus GRAPEFRUIT;
>   //...
> 
>  protected:
>   Citrus() {}
> };
> 
> class Fruit : Citrus
> { static const Fruit APPLE;
>   //...
>  protected:
>   Fruit() {}
> };
> 
> bool operator==(const Citrus& l, const Citrus& r)
> { return &l == &r;
> }
> bool operator!=(const Citrus& l, const Citrus& r)
> { return &l != &r;
> }
> 
> Note that this pseudo enums have no value, since the fixed number of
> instances is already sufficient for uniqueness. This is similar to Java
> 2 like enums.
[snip]

Hi Marcel,

I tried a slightly modified form of your code shown in the attached.
However, trying to compile it, without the #define DEF_FRUITS, I got the
error messages:

./build/gcc4_6v/gcc.test/enum_static_const.o: In function `main':
/home/evansl/prog_dev/gcc.test/enum_static_const.cpp:37: undefined
reference to `Fruit::APPLE'
/home/evansl/prog_dev/gcc.test/enum_static_const.cpp:38: undefined
reference to `Citrus::ORANGE'
collect2: ld returned 1 exit status

In addition, I'm guessing you meant the copy CTOR's should be private
too to avoid any other instances of Fruit or Citrus from being
created.  Is that right?

-regards,
Larry

[toc] | [prev] | [next] | [standalone]


#13696

FromMarcel Müller <news.5.maazl@spamgourmet.com>
Date2012-02-01 21:14 +0100
Message-ID<4f299d0e$0$6569$9b4e6d93@newsspool3.arcor-online.net>
In reply to#13693
On 01.02.2012 18:46, Larry Evans wrote:
> I tried a slightly modified form of your code shown in the attached.
> However, trying to compile it, without the #define DEF_FRUITS, I got the
> error messages:
>
> ./build/gcc4_6v/gcc.test/enum_static_const.o: In function `main':
> /home/evansl/prog_dev/gcc.test/enum_static_const.cpp:37: undefined
> reference to `Fruit::APPLE'
> /home/evansl/prog_dev/gcc.test/enum_static_const.cpp:38: undefined
> reference to `Citrus::ORANGE'
> collect2: ld returned 1 exit status

You are right. It was no complete code. It only shows the way.

C++ requires statics to be defined outside the class in a compilation 
unit. I.e.:

const Citrus Citrus::ORANGE;
const Citrus Citrus::GRAPEFRUIT;
const Fruit Fruit::APPLE;
...


> In addition, I'm guessing you meant the copy CTOR's should be private
> too to avoid any other instances of Fruit or Citrus from being
> created.  Is that right?

Yes, both. The default constructor need to be private and the object 
should be non-copyable.
   Citrus(const Citrus&) = delete;
   Citrus& operator=(const Citrus&) = delete;
should do the job.


Marcel

[toc] | [prev] | [next] | [standalone]


#13689

FromThomas Boell <tboell@domain.invalid>
Date2012-02-01 14:08 +0100
Message-ID<20120201140814.3c116fb2@arcadia>
In reply to#13679
On Tue, 31 Jan 2012 14:56:03 -0800 (PST)
Christopher <cpisz@austin.rr.com> wrote:

> A developer before me created a large enumeration we can call
> EnumType1.
> He then, right under it, typedef-ed another
> 
> typedef EnumType1 EnumType2
> 
> He then created a comment "alias, should only include x types", where
> x is a rule defining a subset of EnumType1
> 
> This is crap imo. Any function or method declared to take EnumType2,
> would happily take a value from  EnumType1 that does not meet the x
> criteria.
> 
> Since I know the subset from EmunType1 that meets the x criteria, how
> can I define a subset EnumType2 that only includes those enums without
> typing the whole darn subset twice?
> 
> Example
> enum Fruit
> {
>    APPLE = 0,
>    BANANA,
>    ORANGE,
>    TANGERINE,
>    GRAPEFRUIT,
>    NUM_FRUITS
> };
> 
> typedef Fruit Citrus;   // This is crap and I want to fix it

You could do something like this:

#define FRUIT_VALUES	\
    APPLE,	\
    BANANA,	\
    ORANGE

enum Fruit
{
	FRUIT_VALUES
};

enum Things
{
	FRUIT_VALUES,
	BEERCAN, CAR, ALOT
};

Whether using the preprocessor like this is "good style" depends on
your point of view.

[toc] | [prev] | [next] | [standalone]


#13697

FromTobias Müller <troplin@bluewin.ch>
Date2012-02-01 21:34 +0000
Message-ID<1898387561349824111.008516troplin-bluewin.ch@news.aioe.org>
In reply to#13689
Thomas Boell <tboell@domain.invalid> wrote:
> You could do something like this:
> 
> #define FRUIT_VALUES	\
>     APPLE,	\
>     BANANA,	\
>     ORANGE
> 
> enum Fruit
> {
> 	FRUIT_VALUES
> };
> 
> enum Things
> {
> 	FRUIT_VALUES,
> 	BEERCAN, CAR, ALOT
> };
> 
> Whether using the preprocessor like this is "good style" depends on
> your point of view.

That's quite dangerous. You have to be sure, that FRUIT_VALUES is always
the first in the enum, otherwise the values are no more the same in both
enums.

I think it should not be possible to have the same name in two different
enums anyway. I am not sure though. 

Tobi

[toc] | [prev] | [next] | [standalone]


#13698

From"Matt D." <nospam@invalid.local>
Date2012-02-02 00:37 +0100
Message-ID<jgciba$f4c$1@portraits.wsisiz.edu.pl>
In reply to#13689
On 2/1/2012 14:08, Thomas Boell wrote:
> You could do something like this:
>
> #define FRUIT_VALUES	\
>      APPLE,	\
>      BANANA,	\
>      ORANGE
>
> enum Fruit
> {
> 	FRUIT_VALUES
> };
>
> enum Things
> {
> 	FRUIT_VALUES,
> 	BEERCAN, CAR, ALOT
> };
>
> Whether using the preprocessor like this is "good style" depends on
> your point of view.

It's seems very much like inheritance, so it made me think that perhaps 
using something along the following could be of use: 
http://www.codeproject.com/Articles/16150/Inheriting-a-C-enum-type

Best,

Matt

[toc] | [prev] | [next] | [standalone]


#13699

FromThomas Boell <tboell@domain.invalid>
Date2012-02-02 02:38 +0100
Message-ID<20120202023846.274d007a@arcadia>
In reply to#13698
On Thu, 02 Feb 2012 00:37:15 +0100
"Matt D." <nospam@invalid.local> wrote:

> On 2/1/2012 14:08, Thomas Boell wrote:
> > You could do something like this:
> >
> > #define FRUIT_VALUES	\
> >      APPLE,	\
> >      BANANA,	\
> >      ORANGE
> >
> > enum Fruit
> > {
> > 	FRUIT_VALUES
> > };
> >
> > enum Things
> > {
> > 	FRUIT_VALUES,
> > 	BEERCAN, CAR, ALOT
> > };
> >
> > Whether using the preprocessor like this is "good style" depends on
> > your point of view.
> 
> It's seems very much like inheritance, so it made me think that perhaps 
> using something along the following could be of use: 
> http://www.codeproject.com/Articles/16150/Inheriting-a-C-enum-type

He forgot to start the NewFruits enum values after the end of Fruit, so
he will not be able to distinguish Apples from Oranges.

[toc] | [prev] | [next] | [standalone]


#13695

FromLarry Evans <cppljevans@suddenlink.net>
Date2012-02-01 12:28 -0600
Message-ID<4f29844d$0$27602$bbae4d71@news.suddenlink.net>
In reply to#13679
On 01/31/12 16:56, Christopher wrote:
> A developer before me created a large enumeration we can call
> EnumType1.
> He then, right under it, typedef-ed another
> 
> typedef EnumType1 EnumType2
> 
> He then created a comment "alias, should only include x types", where
> x is a rule defining a subset of EnumType1
> 
> This is crap imo. Any function or method declared to take EnumType2,
> would happily take a value from  EnumType1 that does not meet the x
> criteria.
> 
> Since I know the subset from EmunType1 that meets the x criteria, how
> can I define a subset EnumType2 that only includes those enums without
> typing the whole darn subset twice?
> 
> Example
> enum Fruit
> {
>    APPLE = 0,
>    BANANA,
>    ORANGE,
>    TANGERINE,
>    GRAPEFRUIT,
>    NUM_FRUITS
> };
> 
> typedef Fruit Citrus;   // This is crap and I want to fix it
> 
> 
> 
Maybe this thread would help:

http://groups.google.com/group/comp.lang.c++/msg/579f71bf8d783ec0

-regards,
Larry

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web