declarator name equals type name weirdness

  • Thread starter Andreas Leitner
  • Start date
A

Andreas Leitner

Hi,

I got a code snipset that gcc refuses to compile. I am note sure however
whether this is a bug in gcc, or the snipset is just invalid C code.

This is the source code in question:
----
typedef int foo;

void bar1 (foo* foo)
{
}

void bar2 (foo foo)
{
bar1((foo*) foo);
}

int main (void)
{
}
---
And this is what gcc 3.3.4 tells me about it:
---
foo.c: In function `bar2':
foo.c:9: error: parse error before ')' token
---

If I remove the cast, gcc compiles the sample just fine. 'foo' is
overloaded to be a type name and parameter in both funciton 'bar1' and
'bar2'. Such overloading seems to be legal ('bar1' alone and 'bar2'
without the cast compile just fine). However the cast doesnt seem to
work. This restriction seems a bit arbitrary to me, is the cast legal C
or not?

I do know that the name overloading is bad style. I extracted the
snipset from generated code.

many thanks in advance,
Andreas
 
J

Jens.Toerring

Andreas Leitner said:
I got a code snipset that gcc refuses to compile. I am note sure however
whether this is a bug in gcc, or the snipset is just invalid C code.
This is the source code in question:
void bar1 (foo* foo)
{
}
void bar2 (foo foo)
{
bar1((foo*) foo);
}
int main (void)
{
}

You can't have the name of a variable being identical to a type, even
a typedef'ed one. Unfortunately, the compiler doesn't complain about
that already in the argument list, but that seems to be because it's
typedef'ed, if you try e.g.

void bar1( int* int )

you get told immediately that there's something fishy.

Regards, Jens
 
A

Andreas Leitner

You can't have the name of a variable being identical to a type, even
a typedef'ed one. Unfortunately, the compiler doesn't complain about
that already in the argument list, but that seems to be because it's
typedef'ed, if you try e.g.

void bar1( int* int )

you get told immediately that there's something fishy.

Are you sure? 'int' is AFAIK a keyword and lives in a different
namespace than typedef typenames. Also the following compiles just fine:
----
typedef int foo;

void bar1 (foo* foo)
{
int* ip = foo;
}

int main (void)
{
}
----

However the following doesnt:
----
typedef int foo;

void bar1 (foo* foo)
{
foo x;
}

int main (void)
{
}
----

So maybe the rule is that once you overload 'foo' as an argument,
(withing the function scope) its not available as a typename anymore.

Andreas
 
L

Lawrence Kirby

Hi,

I got a code snipset that gcc refuses to compile. I am note sure however
whether this is a bug in gcc, or the snipset is just invalid C code.

It is invalid C code.
This is the source code in question:
----
typedef int foo;

void bar1 (foo* foo)
{
}

void bar2 (foo foo)
{
bar1((foo*) foo);
}

int main (void)
{
}
---
And this is what gcc 3.3.4 tells me about it:
---
foo.c: In function `bar2':
foo.c:9: error: parse error before ')' token
---

If I remove the cast, gcc compiles the sample just fine. 'foo' is
overloaded to be a type name and parameter in both funciton 'bar1' and
'bar2'.

There is no overloading here. foo is defined as a typedef name at an outer
scope and a variable name at an inner scope. Inner scope declarations hide
outer scope ones within their scope. So the foo in the cast refers to the
variable foo, not the type foo hence the error. This is like writing

void bar2 (foo foo2)
{
bar1((foo2*) foo2);
}

Lawrence
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top