K
Keith Thompson
Rui Maciel said:Do you see the ability to abstract types as being a benefit which is minor
enough that is not worth doing?
No, I overlooked that; see my followup to Tim Rentsch.
Rui Maciel said:Do you see the ability to abstract types as being a benefit which is minor
enough that is not worth doing?
Yes, if you want an opaque type, a typedef is perfectly appropriate; I
should have mentioned that. By "opaque" in this case, I mean that the
code that uses the type doesn't need to know or care that the type is a
struct.
I haven't seen a whole lot of code like that.
You mean arguments like "I don't like using typedefs"?
Originally integers were used as file ids. This is still the systemWhat about FILE? Is there any implementation where that "opaque" type
_isn't_ actually a struct?
What about FILE? Is there any implementation where that "opaque" type
_isn't_ actually a struct?
Malcolm McLean said:Originally integers were used as file ids. This is still the system
with Fortran. Hence stdin, stdout and stderr could be simple defines.
Ben Bacarisse said:I don't see how that is supposed to work:
#include <stdio.h>
typedef __uint128_t uint128_t;
#define UINT128_C(X) ((uint128_t) (X))
int main(void)
{
uint128_t j = UINT128_C(0x10000000000000000ULL);
puts(j ? "ok" : "fail");
return 0;
}
prints "fail" (with a compiler-time warning about the constant being too
large for its type).
The UINTx_C macros usually need compiler magic and the manual says:
"There is no support in GCC to express an integer constant of type
__int128 for targets having long long integer with less then 128 bit
width"
Phil Carmody said:Late to the party. Usenet is an occasional treat currently.
Don't force it to be a ULL before casting it to the 128-bit type then?
Ben Bacarisse said:I don't see how that helps. It certainly doesn't alter the behaviour of
the example program when compiler with gcc.
Pics or it didn't happen.I'd argue that there is much evidence for the existence of a God.
Keith Thompson said:True. But that creates an artificial distinction between
self-referential and non-self-referential structs. My point is
that if you have a tag, you don't need a typedef.
Yes, if you want an opaque type, a typedef is perfectly appropriate; I
should have mentioned that. By "opaque" in this case, I mean that the
code that uses the type doesn't need to know or care that the type is a
struct.
I haven't seen a whole lot of code like that. It's more common, at
least in code I've seen, to declare a typedef for a struct type and then
write code that freely refers to its members.
Bad editing on my part; delete "are different identifiers".
Too may programmers *don't* follow any particular convention.
I see plenty of code like:
typedef struct foo {
/* ... */
} bar;
where the names "foo" and "bar" don't have any particular relationship.
Strictly following some naming convention (like "typedef struct foo_s {
... } foo") is better, but that's still two different identifiers to
keep track of. Using the same identifier for both is even better.
I mentioned it later: it gives the type a name that's a single
identifier, and saves a little typing.
[...]To my way of thinking the main reason to use typedef names
is to encourage use of information hiding and viewing of
various entities as abstract data types.
I agree that that's the main *appropriate* reason to use typedefs for
structs. Unfortunately, it's not the main reason programmers actually
use them.
[on typedef structs]Tim Rentsch said:Keith Thompson <[email protected]> writes:
More importantly, it saves horizontal space, which can be
important in places like function prototypes.
Tim Rentsch said:Keith Thompson said:[...]To my way of thinking the main reason to use typedef names
is to encourage use of information hiding and viewing of
various entities as abstract data types.
I agree that that's the main *appropriate* reason to use typedefs for
structs. Unfortunately, it's not the main reason programmers actually
use them.
But that's a reason to talk about information hiding, ADT's, and
opaque types /more/, not to insist more strenuously that typedefs
shouldn't be used. If you show examples of where it's good to
typedef a struct, that will both strengthen an understanding of
information hiding and ADT's, etc, and also make comments about
when /not/ to use typedefs more effective. Always typedef'ing
is bad; never typedef'ing is bad. A good developer needs to
understand the problems of both extremes.
[on typedef structs]Tim Rentsch said:Keith Thompson<[email protected]> writes:
More importantly, it saves horizontal space, which can be
important in places like function prototypes.
Pet peeve: why can't C's usual declaration syntax be used in function
prototypes (and definitions)? I.e.:
int memcmp(const void *s1, *s2; size_t n);
It seems a shame to me. I wonder if it was every considered.
[on typedef structs]Tim Rentsch said:Keith Thompson<[email protected]> writes:
I mentioned it later: it gives the type a name that's a single
identifier, and saves a little typing.
More importantly, it saves horizontal space, which can be
important in places like function prototypes.
Pet peeve: why can't C's usual declaration syntax be used in function
prototypes (and definitions)? I.e.:
int memcmp(const void *s1, *s2; size_t n);
It seems a shame to me. I wonder if it was every considered.
It doesn't work very well if the parameters aren't named in a function
declaration. You can't have a nameless variable declaration, but you
Kaz Kylheku said:[on typedef structs]
I mentioned it later: it gives the type a name that's a single
identifier, and saves a little typing.
More importantly, it saves horizontal space, which can be
important in places like function prototypes.
Pet peeve: why can't C's usual declaration syntax be used in function
prototypes (and definitions)? I.e.:
int memcmp(const void *s1, *s2; size_t n);
It seems a shame to me. I wonder if it was every considered.
It doesn't work very well if the parameters aren't named in a function
declaration. You can't have a nameless variable declaration, but you
Is there a syntactic issue with a nameless declaration with multiple
abstract declarators like void *, *, **; ? Off the top of my head,
I can't see it.
[...] the most
important use case for typedef'ing a struct is to make a
(mostly) opaque type,
That's easy to believe, and I think part of the reason for that
happening is lots of people giving blanket advice like "don't use
typedefs for structs" rather than talking about opaque types and
why it's good to use them. Hearing so many comments about not
using typedefs, it's easy for people to come away with the wrong
lesson.
Actually I don't agree with this last piece of advice, but it
should be mostly irrelevant because properly done a struct
that is typedef'ed has its tag appear in a very small number of
places, ideally just two or three.
Promoting opaque types, and information hiding, are two of the
most important.
I mentioned it later: it gives the type a name that's a single
identifier, and saves a little typing.[...]To my way of thinking the main reason to use typedef names
is to encourage use of information hiding and viewing of
various entities as abstract data types.
I agree that that's the main *appropriate* reason to use typedefs for
structs. Unfortunately, it's not the main reason programmers actually
use them.
But that's a reason to talk about information hiding, ADT's, and
opaque types /more/, not to insist more strenuously that typedefs
shouldn't be used. If you show examples of where it's good to
typedef a struct, that will both strengthen an understanding of
information hiding and ADT's, etc, and also make comments about
when /not/ to use typedefs more effective.
yes but its a long ugly name.
The C++ people seem to agree, once you've defined a struct you don't
have to scatter "struct" keywords about the place.
Keith Thompson said:Tim Rentsch said:Keith Thompson said:To my way of thinking the main reason to use typedef names
is to encourage use of information hiding and viewing of
various entities as abstract data types.
[...]
I agree that that's the main *appropriate* reason to use typedefs for
structs. Unfortunately, it's not the main reason programmers actually
use them.
But that's a reason to talk about information hiding, ADT's, and
opaque types /more/, not to insist more strenuously that typedefs
shouldn't be used. [snip]
Ok, let me refine my stated position. [snip]
but that's not the only reason that people typedef structs. I do it
becasue I think the syntax is poor. I hide other hard-to-read
declaration syntax behind typedefs for the same reason. I don't do it
because I got confused about opaque types.
So I don't think they have "learned the wrong lesson"
[confusion about naming between tags and typedef names]
To my way of thinking the main reason to use typedef names
is to encourage use of information hiding and viewing of
various entities as abstract data types.
[...]
I agree that that's the main *appropriate* reason to use typedefs for
structs. Unfortunately, it's not the main reason programmers actually
use them.
But that's a reason to talk about information hiding, ADT's, and
opaque types /more/, not to insist more strenuously that typedefs
shouldn't be used. If you show examples of where it's good to
typedef a struct, that will both strengthen an understanding of
information hiding and ADT's, etc, and also make comments about
when /not/ to use typedefs more effective.
always supposing the heavy typedefers actually /are/ confused
To my way of thinking the main reason to use typedef names
is to encourage use of information hiding and viewing of
various entities as abstract data types. In most cases, as
far as client code codes, the less one knows about a data
type the better; similarly, the more a type is treated as
an abstract data type, the better. The main cost of using
raw struct types is not in the typing but in the thinking:
for most code, not only do I not want to know what sort of
thing is being handled, I want NOT to know, because knowing
clutters ones thinking. Using a typedef name doesn't
prevent me from knowing (since after all I can go look at
the typedef in whatever header file defines it), but it
makes it easy not to know if one is so inclined. (And
those not so inclined can always just go look...)
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.