enum and struct namespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grumble

    #1

    enum and struct namespaces

    Hello,

    I thought names of enums and names of structs lived in different
    namespaces, in C89. Am I mistaken? It would seem so:

    $ cat testcase.c
    struct foobar { int x,y,z; };
    enum foobar {AAA, BBB, CCC};
    int main(void) { return 0; }

    $ gcc-3.4.3 -Wall -ansi -pedantic testcase.c
    testcase.c:2: error: `foobar' defined as wrong kind of tag

    $ icc-8.0 -strict_ansi testcase.c
    testcase.c(2): error: "foobar" has already been declared in the current
    scope
    enum foobar {AAA, BBB, CCC};
    ^

    compilation aborted for testcase.c (code 2)

    It seems the correct term is 'scope' instead of 'namespace'? I don't
    understand why the standard would impose such a limitation? It seems
    trivial to determine that 'struct foobar' and 'enum foobar' are two
    unrelated types?

    --
    Regards, Grumble
  • Michael Mair

    #2
    Re: enum and struct namespaces



    Grumble wrote:[color=blue]
    > Hello,
    >
    > I thought names of enums and names of structs lived in different
    > namespaces, in C89. Am I mistaken?[/color]

    Yes, you are.
    Tags for structures, unions and enumerations live all in the
    same namespace.
    [color=blue]
    > It would seem so:
    >
    > $ cat testcase.c
    > struct foobar { int x,y,z; };
    > enum foobar {AAA, BBB, CCC};
    > int main(void) { return 0; }
    >
    > $ gcc-3.4.3 -Wall -ansi -pedantic testcase.c
    > testcase.c:2: error: `foobar' defined as wrong kind of tag
    >
    > $ icc-8.0 -strict_ansi testcase.c
    > testcase.c(2): error: "foobar" has already been declared in the current
    > scope
    > enum foobar {AAA, BBB, CCC};
    > ^
    >
    > compilation aborted for testcase.c (code 2)
    >
    > It seems the correct term is 'scope' instead of 'namespace'?[/color]

    Obviously, you can use the same tag for completely unrelated
    scopes.
    [color=blue]
    > I don't
    > understand why the standard would impose such a limitation? It seems
    > trivial to determine that 'struct foobar' and 'enum foobar' are two
    > unrelated types?[/color]

    The limitation does not make much sense but it is there.
    In C99, too.


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.

    Comment

    • Mark A. Odell

      #3
      Re: enum and struct namespaces

      Grumble <devnull@kma.eu .org> wrote in
      news:cppeo0$sj8 $1@news-rocq.inria.fr:
      [color=blue]
      > Hello,
      >
      > I thought names of enums and names of structs lived in different
      > namespaces, in C89.[/color]

      Maybe you are thinking about typedef and enum/struct/union tags?

      --
      - Mark ->
      --

      Comment

      • Grumble

        #4
        Re: enum and struct namespaces

        Grumble wrote:
        [color=blue]
        > It seems the correct term is 'scope' instead of 'namespace'?[/color]

        Please disregard this particular question.

        (My brain must have gone numb for a minute.)

        --
        Regards, Grumble

        Comment

        Working...