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


Groups > comp.lang.c > #18861 > unrolled thread

reusing identifier in two enums

Started byImpalerCore <jadill33@gmail.com>
First post2012-03-19 08:04 -0700
Last post2012-03-21 17:04 +0100
Articles 7 — 5 participants

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


Contents

  reusing identifier in two enums ImpalerCore <jadill33@gmail.com> - 2012-03-19 08:04 -0700
    Re: reusing identifier in two enums "Morris Keesan" <mkeesan@post.harvard.edu> - 2012-03-19 12:19 -0400
      Re: reusing identifier in two enums ImpalerCore <jadill33@gmail.com> - 2012-03-19 10:43 -0700
    Re: reusing identifier in two enums Keith Thompson <kst-u@mib.org> - 2012-03-19 10:43 -0700
      Re: reusing identifier in two enums ImpalerCore <jadill33@gmail.com> - 2012-03-19 11:42 -0700
    Re: reusing identifier in two enums jgk@panix.com (Joe keane) - 2012-03-21 02:45 +0000
      Re: reusing identifier in two enums Jens Gustedt <jens.gustedt@loria.fr> - 2012-03-21 17:04 +0100

#18861 — reusing identifier in two enums

FromImpalerCore <jadill33@gmail.com>
Date2012-03-19 08:04 -0700
Subjectreusing identifier in two enums
Message-ID<93a85ab3-ecbf-4aa0-8fbd-6699e810b9e9@v2g2000vbx.googlegroups.com>
I have two related but separate enumerations where I'd like to use the
same identifier to represent an invalid type.

enum typeA
{
  invalid_type = -1,
  typeA = 0,
  typeB = 1,
  ...
};

enum type0
{
  invalid_type = -1,
  type0 = 0,
  type1 = 1,
  ...
};

Is it okay to for these enumerations to share the same identifier as
long as they have the same value in all enumerations that use them?
From my limited experience with the standard, I couldn't pull out
whether this was defined behavior.

[toc] | [next] | [standalone]


#18865

From"Morris Keesan" <mkeesan@post.harvard.edu>
Date2012-03-19 12:19 -0400
Message-ID<op.wbfebhni5qv6o3@toshiba-laptop>
In reply to#18861
On Mon, 19 Mar 2012 11:04:40 -0400, ImpalerCore <jadill33@gmail.com> wrote:

> I have two related but separate enumerations where I'd like to use the
> same identifier to represent an invalid type.
>
> enum typeA
> {
>   invalid_type = -1,
>   typeA = 0,
>   typeB = 1,
>   ...
> };
>
> enum type0
> {
>   invalid_type = -1,
>   type0 = 0,
>   type1 = 1,
>   ...
> };
>
> Is it okay to for these enumerations to share the same identifier as
> long as they have the same value in all enumerations that use them?
> From my limited experience with the standard, I couldn't pull out
> whether this was defined behavior.

It looks like it's forbidden.  From the 1999 standard, 6.7.2.2 Enumeration  
Specifiers, paragraph 3 says, "The identifiers in an enumerator list are
declared as constants that have type int and may appear wherever such are
permitted.107)" and footnote 107 says, "107) Thus, the identifiers of
enumeration constants declared in the same scope shall all be distinct from
each other and from other identifiers declared in ordinary declarators."

The version of gcc I have on my machine agrees:
     error: redeclaration of enumerator 'invalid_type'
-- 
Morris Keesan -- mkeesan@post.harvard.edu

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


#18870

FromImpalerCore <jadill33@gmail.com>
Date2012-03-19 10:43 -0700
Message-ID<09eb4349-ea89-4eac-860e-5a77ecb04486@gw9g2000vbb.googlegroups.com>
In reply to#18865
On Mar 19, 12:19 pm, "Morris Keesan" <mkee...@post.harvard.edu> wrote:
> On Mon, 19 Mar 2012 11:04:40 -0400, ImpalerCore <jadil...@gmail.com> wrote:
> > I have two related but separate enumerations where I'd like to use the
> > same identifier to represent an invalid type.
>
> > enum typeA
> > {
> >   invalid_type = -1,
> >   typeA = 0,
> >   typeB = 1,
> >   ...
> > };
>
> > enum type0
> > {
> >   invalid_type = -1,
> >   type0 = 0,
> >   type1 = 1,
> >   ...
> > };
>
> > Is it okay to for these enumerations to share the same identifier as
> > long as they have the same value in all enumerations that use them?
> > From my limited experience with the standard, I couldn't pull out
> > whether this was defined behavior.
>
> It looks like it's forbidden.  From the 1999 standard, 6.7.2.2 Enumeration
> Specifiers, paragraph 3 says, "The identifiers in an enumerator list are
> declared as constants that have type int and may appear wherever such are
> permitted.107)" and footnote 107 says, "107) Thus, the identifiers of
> enumeration constants declared in the same scope shall all be distinct from
> each other and from other identifiers declared in ordinary declarators."
>
> The version of gcc I have on my machine agrees:
>      error: redeclaration of enumerator 'invalid_type'

I got the same compiler error.  I guess I didn't notice the duplicate
since I declared the identifier in two separate header files but I
never had any test code that directly used it.

Thanks,
John D.

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


#18869

FromKeith Thompson <kst-u@mib.org>
Date2012-03-19 10:43 -0700
Message-ID<lnmx7cij0j.fsf@nuthaus.mib.org>
In reply to#18861
ImpalerCore <jadill33@gmail.com> writes:
> I have two related but separate enumerations where I'd like to use the
> same identifier to represent an invalid type.
>
> enum typeA
> {
>   invalid_type = -1,
>   typeA = 0,
>   typeB = 1,
>   ...
> };
>
> enum type0
> {
>   invalid_type = -1,
>   type0 = 0,
>   type1 = 1,
>   ...
> };
>
> Is it okay to for these enumerations to share the same identifier as
> long as they have the same value in all enumerations that use them?
> From my limited experience with the standard, I couldn't pull out
> whether this was defined behavior.

It's a constraint violation, assuming both types are declared in the
same scope.

Quoting N1570:

6.7.2.2:
    The identifiers in an enumerator list are declared as constants that
    have type int and may appear wherever such are permitted.

So your declaration of invalid_type is equivalent to:

    const int invalid_type = -1;

A footnote makes it even clearer:

    Thus, the identifiers of enumeration constants declared in the same
    scope shall all be distinct from each other and from other
    identifiers declared in ordinary declarators.

And 6.7p3 says:

    If an identifier has no linkage, there shall be no more than one
    declaration of the identifier (in a declarator or type specifier)
    with the same scope and in the same name space, except that:
    [...]
(The exceptions don't apply in this case.)

This is a constraint, so a diagnostic is required.  A compiler *could*
issue a warning and accept the declaration anyway, but I don't know of
any that do so.

If you had tried to compile your example, the compiler would have
rejected it; for example, gcc gives me:

    c.c:10:18: error: redeclaration of enumerator ‘invalid_type’
    c.c:3:18: note: previous definition of ‘invalid_type’ was here

This doesn't prove that it's invalid (it *could* be a compiler bug), but
it's a strong indication.

You could declare the "invalid_type" enumeration constant with a
different name in each enum type, then declare a single:

    enum { invalid_type = -1 };

and use that identifier everywhere.  Enumeration constants aren't as
strongly associated with their types as you might expect; they're all of
type int, so there's no real type checking.  (C++ has different rules,
so if you want your code to be compatible with C++ for some reason
you'll have to be more careful.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


#18873

FromImpalerCore <jadill33@gmail.com>
Date2012-03-19 11:42 -0700
Message-ID<99a4a34e-15f4-4b64-a720-603c60877c56@l7g2000vbw.googlegroups.com>
In reply to#18869
On Mar 19, 1:43 pm, Keith Thompson <ks...@mib.org> wrote:

[snip]

> You could declare the "invalid_type" enumeration constant with a
> different name in each enum type, then declare a single:
>
>     enum { invalid_type = -1 };
>
> and use that identifier everywhere.  Enumeration constants aren't as
> strongly associated with their types as you might expect; they're all of
> type int, so there's no real type checking.  (C++ has different rules,
> so if you want your code to be compatible with C++ for some reason
> you'll have to be more careful.)

Thanks for the idea.  I'll keep it in mind.

Best regards,
John D.

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


#18919

Fromjgk@panix.com (Joe keane)
Date2012-03-21 02:45 +0000
Message-ID<jkbfc7$ep8$1@reader1.panix.com>
In reply to#18861
In article <93a85ab3-ecbf-4aa0-8fbd-6699e810b9e9@v2g2000vbx.googlegroups.com>,
ImpalerCore  <jadill33@gmail.com> wrote:
>I have two related but separate enumerations where I'd like to use the
>same identifier to represent an invalid type.

This is basically subtypes which define the valid values, and a
supertype where one can only set an invalid value, and test for that.

class basic_type
{
    static const int invalid = -1;
    int x;

    bool is_valid() const { return x >= 0; }
    void set_invalid() { x = invalid; }
};

class color_type : basic_type
{
    enum color { red = 1, orange = 13, yellow = 3, ... };

    bool is_red() const { return x == red; }
    void set_red() { x = red; }
    ...
};

Decrement the language:

#define BASIC_INVALID_TYPE -1
#define BASIC_TYPE_IS_VALID(X) ((X) >= 0)
#define BASIC_TYPE_SET_INVALID(X) ((X) = BASIC_INVALID_TYPE)

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


#18930

FromJens Gustedt <jens.gustedt@loria.fr>
Date2012-03-21 17:04 +0100
Message-ID<4F69FC1E.8090506@loria.fr>
In reply to#18919
Am 03/21/2012 03:45 AM, schrieb Joe keane:
> In article <93a85ab3-ecbf-4aa0-8fbd-6699e810b9e9@v2g2000vbx.googlegroups.com>,
> ImpalerCore  <jadill33@gmail.com> wrote:
>> I have two related but separate enumerations where I'd like to use the
>> same identifier to represent an invalid type.
> 
> This is basically subtypes which define the valid values, and a
> supertype where one can only set an invalid value, and test for that.

subtype ?

> class basic_type
> {
>     static const int invalid = -1;
>     int x;
> 
>     bool is_valid() const { return x >= 0; }
>     void set_invalid() { x = invalid; }
> };
> 
> class color_type : basic_type
> {
>     enum color { red = 1, orange = 13, yellow = 3, ... };
> 
>     bool is_red() const { return x == red; }
>     void set_red() { x = red; }
>     ...
> };

wrong language? C doesn't know anything of "class", inheritance and
things like that

> Decrement the language:

You mean C-- ?

Jens

[toc] | [prev] | [standalone]


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


csiph-web