Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > enum members namespace clash

Reply
Thread Tools

enum members namespace clash

 
 
Bilgehan.Balban@gmail.com
Guest
Posts: n/a
 
      06-02-2005
Hi,

I have two different enum definitions with members of same name. The
compiler complains about duplicate definitions. Is this expected
behaviour?

Can't I have same-named fields in different enum definitions? I think
it should have been perfectly valid to do that. Its a stupid limitation
to have to define disjoint enum symbol definitions. This is like having
to have disjoint member names in structures.

Bahadir

 
Reply With Quote
 
 
 
 
Paul Mesken
Guest
Posts: n/a
 
      06-02-2005
On 2 Jun 2005 09:12:04 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>Hi,
>
>I have two different enum definitions with members of same name. The
>compiler complains about duplicate definitions. Is this expected
>behaviour?


Yes, enumeration constants are in one and the same namespace (which
they share with functions, variables and typedef names).

See 1.29 from the FAQ.


 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      06-02-2005
(E-Mail Removed) wrote:
>
> Hi,
>
> I have two different enum definitions with members of same name. The
> compiler complains about duplicate definitions. Is this expected
> behaviour?
>
> Can't I have same-named fields in different enum definitions? I think
> it should have been perfectly valid to do that.
> Its a stupid limitation
> to have to define disjoint enum symbol definitions.
> This is like having
> to have disjoint member names in structures.


Not really. You have no way of knowing which enum
a member belongs to, if members have the same name.

enum forward {ZERO, ONE};
enum backward{ONE, ZERO};

return ZERO;

Perhaps,
if your enum members have the same name at the same value,
you could do better with only one enum?

--
pete
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      06-02-2005
(E-Mail Removed) writes:

> I have two different enum definitions with members of same name. The
> compiler complains about duplicate definitions. Is this expected
> behaviour?


Yes.

> Can't I have same-named fields in different enum definitions?


No.

> I think it should have been perfectly valid to do that. Its a
> stupid limitation to have to define disjoint enum symbol
> definitions. This is like having to have disjoint member names
> in structures.


You could always switch to C++. It has features that can do what
you want. Short of that, the standard practice is to use a
unique prefix for each enumerated type.
--
Ben Pfaff
email: (E-Mail Removed)
web: http://benpfaff.org
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      06-02-2005
(E-Mail Removed) wrote:
> Hi,
>
> I have two different enum definitions with members of same name. The
> compiler complains about duplicate definitions. Is this expected
> behaviour?
>
> Can't I have same-named fields in different enum definitions? I think
> it should have been perfectly valid to do that. Its a stupid limitation
> to have to define disjoint enum symbol definitions. This is like having
> to have disjoint member names in structures.
>
> Bahadir
>


Enumeration constants in C are of type `int' and enumeration constant
identifiers are unqualified when used. If the same identifier occurs in
different enumerations there is no way for the compiler to know which
one to use.


-- August
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      06-02-2005
August Karlstrom wrote:
> (E-Mail Removed) wrote:
>
>> Hi,
>>
>> I have two different enum definitions with members of same name. The
>> compiler complains about duplicate definitions. Is this expected
>> behaviour?
>>
>> Can't I have same-named fields in different enum definitions? I think
>> it should have been perfectly valid to do that. Its a stupid limitation
>> to have to define disjoint enum symbol definitions. This is like having
>> to have disjoint member names in structures.
>>
>> Bahadir
>>

>
> Enumeration constants in C are of type `int' and enumeration constant
> identifiers are unqualified when used. If the same identifier occurs in
> different enumerations there is no way for the compiler to know which
> one to use.


Which brings us to the question why enumerations in C aren't qualified.
Probably because that way things would get rather verbose:

f(SomeEnum.A_CONSTANT | SomeEnum.ANOTHER_CONSTANT
| SomeEnum.A_THIRD_CONSTANT);

rather than

f(A_CONSTANT | ANOTHER_CONSTANT | A_THIRD_CONSTANT);


-- August
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      06-02-2005
(E-Mail Removed) wrote:
> Hi,
>
> I have two different enum definitions with members of same name. The
> compiler complains about duplicate definitions. Is this expected
> behaviour?
>
> Can't I have same-named fields in different enum definitions? I think
> it should have been perfectly valid to do that. Its a stupid limitation
> to have to define disjoint enum symbol definitions. This is like having
> to have disjoint member names in structures.


Another strategy is to forget the `enum' feature and use plain integer
constants (I use double quotes for these pseudo enumerations from now
on). As long as all "enumeration" constants have distinct values, there
is no problem with sharing a constant between different "enumerations".
This is also a safer approach since if a client uses an "enumeration"
constant from the wrong "enumeration" it will be detected.


-- August
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-02-2005
August Karlstrom <(E-Mail Removed)> writes:
[...]
> Which brings us to the question why enumerations in C aren't
> qualified. Probably because that way things would get rather verbose:
>
> f(SomeEnum.A_CONSTANT | SomeEnum.ANOTHER_CONSTANT
> | SomeEnum.A_THIRD_CONSTANT);
>
> rather than
>
> f(A_CONSTANT | ANOTHER_CONSTANT | A_THIRD_CONSTANT);


The language *could* have been defined so that an enumeration constant
can be used without qualification if there's no ambiguity, with some
kind of qualification syntax to be used if there's another constant of
the same name. There are languages that do this. C, unsurprisingly,
isn't one of them.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      06-02-2005
Keith Thompson wrote:
> August Karlstrom <(E-Mail Removed)> writes:
> [...]
>
>>Which brings us to the question why enumerations in C aren't
>>qualified. Probably because that way things would get rather verbose:
>>
>> f(SomeEnum.A_CONSTANT | SomeEnum.ANOTHER_CONSTANT
>> | SomeEnum.A_THIRD_CONSTANT);
>>
>>rather than
>>
>> f(A_CONSTANT | ANOTHER_CONSTANT | A_THIRD_CONSTANT);

>
>
> The language *could* have been defined so that an enumeration constant
> can be used without qualification if there's no ambiguity, with some
> kind of qualification syntax to be used if there's another constant of
> the same name. There are languages that do this. C, unsurprisingly,
> isn't one of them.


What language do you have in mind?

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-03-2005
August Karlstrom <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> August Karlstrom <(E-Mail Removed)> writes:
>> [...]
>>
>>>Which brings us to the question why enumerations in C aren't
>>>qualified. Probably because that way things would get rather verbose:
>>>
>>> f(SomeEnum.A_CONSTANT | SomeEnum.ANOTHER_CONSTANT
>>> | SomeEnum.A_THIRD_CONSTANT);
>>>
>>>rather than
>>>
>>> f(A_CONSTANT | ANOTHER_CONSTANT | A_THIRD_CONSTANT);

>> The language *could* have been defined so that an enumeration
>> constant
>> can be used without qualification if there's no ambiguity, with some
>> kind of qualification syntax to be used if there's another constant of
>> the same name. There are languages that do this. C, unsurprisingly,
>> isn't one of them.

>
> What language do you have in mind?


<OT>
Ada.

Given a type declaration
type Enum is (Zero, One, Two);
each constant is equivalent to a function with no parameters returning
Enum. (Ada doesn't use empty parentheses for parameterless function
calls.) Enumeration constants aren't really implemented as functions,
and they can be used in constant expressions, but it allows all the
overloading rules that apply to functions to apply to enumeration
constants.
</OT>

Using the same approach in C would violate the "Spirit of C", I think.
Using another approach that's specific to enum types (and inventing a
syntax for the qualification) probably wouldn't be worth the effort.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apparent namespace clash Safalra Javascript 2 07-12-2006 10:21 PM
Namespace clash for istream object :-( vivek.astvansh@gmail.com C++ 5 05-10-2006 07:07 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments