473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

declarator name equals type name weirdness

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

Nov 14 '05 #1
3 1650
Andreas Leitner <no****@sbox.tugraz.at> wrote:
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
---


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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
On Mon, 2004-12-06 at 14:14 +0000, Je***********@physik.fu-berlin.de
wrote:

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

Nov 14 '05 #3
On Mon, 06 Dec 2004 14:30:53 +0100, Andreas Leitner wrote:
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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: xxx | last post by:
I am having difficulty seeing why cv_qualifier, which is used as a type specifier in the declaration section of the ISO/IEC grammar, is defined in the declarator section. A declaration can consist...
2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
17
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
0
by: Alex Sedow | last post by:
Standart describe grammar for events as (in EBNF): event-declaration: "event" type variable-declarators ";" "event" type member-name "{" event-accessor-declarations "}" ...
7
by: anjogasa | last post by:
In the midst of reading "The C++ Programming Language", the latest edition by Stroustrup, and I find myself butting my head against a few paragraphs. I have searched the errata on the website, and...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
5
by: Remco van Engelen | last post by:
Hello, I have a question regarding the ISO C grammar. The syntax of a direct-declarator reads (section A.2.2, page 413 in my copy; the (R1) is just to 'name' the rule for later reference): ...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
10
by: mdh | last post by:
Sorry in advance if this is really dumb, but I am trying to get my head around exactly what the declarator is. In the FAQ, 1.21, part of it says "C declarations ....come in 2 parts, a base type and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.