[sldev] Re: -Werror problem in trunk
Mana Janus
mana.janus at web.de
Thu Aug 21 10:13:11 PDT 2008
Robin Cornelius wrote:
> On Thu, Aug 21, 2008 at 5:28 PM, Mana Janus <mana.janus at web.de> wrote:
> Jason
>
>> Absolutely, Jason, if you are using C++ only. "typedef struct { ... }
>> Vertex" is the old C way of doing it.
>> In C++ the simple "struct Vertex { ... }" has the exact same effect and
>> should be preferred, when the header is never included from C.
>> After all, you are not doing "typedef class { ... } Name", are you? ;-)
>>
>>
>
> Now i'm really confused ;-) but would like to understand this fully.
> Sorry to be stupid here..
>
> Are you saying that simply saying struct Vertex { ... } is all that is needed ?
>
> and this is the C++ equivalent of typedef struct { ... } Vertex ,
> which is still valid but deprecated unless the header is to be used as
> a general purpose header (eg C may use it)
>
> and typedef struct Vertex {...} is actually wrong but many compilers
> accept it but gcc 4.3.1 now rejects it as invalid?
>
> Thanks
>
> Robi
Well, in C "struct Vertex" and "Vertex" are two different types.
struct Vertex { ... }; // C: defines type "struct Vertex"
typedef int Vertex; // C: defines different type "Vertex"
This is perfectly legal and meaningful in C. So, if you do not want to
write "struct Vertex" each time, you need to use a typedef.
In C you could even do:
typedef struct Vertex { ... } Vertex; // C: defines two types
"struct Vertex" and "Vertex".
Now, in C++ "struct Vertex" and "Vertex" are the same type, as "class
Vertex2" and "Vertex2" are.
So the examples above are illegal in C++, as they define type Vertex twice.
The preferred way in C++ is simply:
struct Vertex { ... }; // C++: defines both "struct Vertex" and
"Vertex" as one type.
To be compatible between C and C++, so you can use the header in both,
you can use the typedef with a different name after "struct":
typedef struct sVertex { ... } Vertex; // C/C++: defines "struct
sVertex" and "Vertex"
Or, if you don't need the "struct sVertex", you can just drop the name
after "struct":
typedef struct { ... } Vertex; // C/C++: defines "Vertex"
Hope this is not more confusing than it helps. :-)
Best regards,
Mana
More information about the SLDev
mailing list