Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why allow class and typedef to use the same name?

1 view
Skip to first unread message

Jia-sen

unread,
Aug 19, 2010, 10:37:26 PM8/19/10
to
We know both class and typedef declare types. I do not understand why
the same name could be used for both a class and a typedef-name---at
least in gcc v4.4.0---as in the following example.

//--------------------------------------
struct X {
struct A {};
typedef int A; // why not conflict?

A m; // why resolve to 'typedef A'?
};

int main() {
X x;
x.m = 1.0; // ok
X::A y; // why resolve to 'struct A'?
y = 1.0; // error

return 0;
}
//----------------------------------------

Thank for your help,
Jason

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Daniel Krügler

unread,
Aug 20, 2010, 5:23:37 AM8/20/10
to
On 20 Aug., 04:37, Jia-sen <baowei2...@gmail.com> wrote:
> We know both class and typedef declare types. I do not understand why
> the same name could be used for both a class and a typedef-name---at
> least in gcc v4.4.0---as in the following example.
>
> //--------------------------------------
> struct X {
> struct A {};
> typedef int A; // why not conflict?

This *should* be ill-formed. If not, this is
a compiler bug. Rationale is 7.1.3/5:

"In a given scope, a typedef specifier shall not be
used to redefine the name of any type declared in
that scope to refer to a different type. [ Example:
class complex { / ... / };
typedef int complex; // error: redefinition
—end example ]"

HTH & Greetings from Bremen,

Daniel Krügler

Marc

unread,
Aug 20, 2010, 5:03:17 PM8/20/10
to
On Aug 20, 11:23 am, Daniel Krügler <daniel.krueg...@googlemail.com>
wrote:

> On 20 Aug., 04:37, Jia-sen <baowei2...@gmail.com> wrote:
>
> > We know both class and typedef declare types. I do not understand why
> > the same name could be used for both a class and a typedef-name---at
> > least in gcc v4.4.0---as in the following example.
>
> > //--------------------------------------
> > struct X {
> > struct A {};
> > typedef int A; // why not conflict?
>
> This *should* be ill-formed. If not, this is
> a compiler bug.

As a matter of fact:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32997

Seungbeom Kim

unread,
Aug 21, 2010, 12:05:48 PM8/21/10
to
On 2010-08-20 02:23, Daniel Krügler wrote:
> On 20 Aug., 04:37, Jia-sen <baowei2...@gmail.com> wrote:
>> We know both class and typedef declare types. I do not understand why
>> the same name could be used for both a class and a typedef-name---at
>> least in gcc v4.4.0---as in the following example.
>>
>> //--------------------------------------
>> struct X {
>> struct A {};
>> typedef int A; // why not conflict?
>
> This *should* be ill-formed. If not, this is
> a compiler bug. Rationale is 7.1.3/5:

You probably meant 7.1.3/3, didn't you? :)

> "In a given scope, a typedef specifier shall not be
> used to redefine the name of any type declared in
> that scope to refer to a different type. [ Example:
> class complex { / ... / };
> typedef int complex; // error: redefinition
> —end example ]"

On the other hand, these are fine:

// object
struct A { };
int A;

// function
struct A { };
int A();

// enumerator
struct A { };
enum { A };

as shown in 9.1/2:

<blockquote>
If a class name is declared in a scope where an object, function,
or enumerator of the same name is also declared, then when both
declarations are in scope, the class can be referred to only using
an elaborated-type-specifier (3.4.4). [Example:

struct stat {
// ...
};

stat gstat; // use plain stat to
// define variable

int stat(struct stat*); // redeclare stat as function

void f()
{
struct stat* ps; // struct prefix needed
// to name struct stat
// ...
stat(ps); // call stat()
// ...
}

—end example]
</blockquote>

--
Seungbeom Kim

0 new messages