enum and struct namespaces

G

Grumble

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?
 
M

Michael Mair

Grumble said:
Hello,

I thought names of enums and names of structs lived in different
namespaces, in C89. Am I mistaken?

Yes, you are.
Tags for structures, unions and enumerations live all in the
same namespace.
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'?

Obviously, you can use the same tag for completely unrelated
scopes.
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?

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


Cheers
Michael
 
M

Mark A. Odell

Hello,

I thought names of enums and names of structs lived in different
namespaces, in C89.

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

Grumble

Grumble said:
It seems the correct term is 'scope' instead of 'namespace'?

Please disregard this particular question.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top