Remco <[email protected]> writes:
> Mike Williams wrote:
>
>> [asbestos suit donned? check!]
>>
>> The style(9) man page contains the statement
>>
>> Don't use the same name for a struct tag and a typedef, as this makes
>> the code unusable from C++.
>>
>> My question is how does this make the code unusable from C++?
...
>
> If I understand you correctly you'd like to do something like:
> typedef struct some_type some_type;
>
> AFAICT when a struct is defined in C++, e.g.:
> struct some_type {
> ...
> };
> variables can be declared as, either:
> struct some_type some_var;
> or:
> some_type some_var;
>
> So 'some_type' is already sort of an implicit typedef of 'struct some_type'.
I'm not sure I like this way of thinking about it. C++ also has typedef
which can be used to create a synonym for a C++ struct, e.g.
struct X;
typedef X Y;
typedef of a sort of typedef. I guess so.
I'm also interested in the answer to this question. A test shows you
can get away with a typedef having the same name as a C++ struct type,
at least in gcc, at least for this simple case:
#include <iostream>
typedef struct X {
int a, b;
} X;
int
main()
{
X x = { 1, 3 };
struct X y = { 7, 4 };
std::cout << x.a << ", " << x.b << "\n";
std::cout << y.a << ", " << y.b << std::endl;
return 0;
}
Section 7.1.3, clause 2 of the 2003 C++ standard also
makes it clear (in as much as this language is clear) it's allowed:
"In a given non-class scope, a typedef specifier can be used to redefine
the name of any type declared in that scope to refer to the type to
which it already refers. [Example:
typedef struct s { /* ... */ } s;
typedef int I;
typedef int I;
typedef I I;
end example]
"
However, that doesn't mean it's good style and there may be some
practical problem with doing it that I'm not familiar with and the man
page writer was familiar with. I don't make it a habit to give new
names to things that are the same as their previous names, so I have no
experience.
- Mike