Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > An excursion to pseudo-polymorphism

Reply
Thread Tools

An excursion to pseudo-polymorphism

 
 
Dave Thompson
Guest
Posts: n/a
 
      07-31-2006
On 21 Jul 2006 22:42:50 -0700, "Bill Pursell" <(E-Mail Removed)>
wrote:
<snip>
> I was recently playing around with some similar ideas and
> had hoped that typedef'ing the void * would generate a
> compiler warning, but was unable to coerce any warnings. eg


Nope. typedef in C is strictly an alias, not a distinct type.

The only way to create a distinct new type in C is as a struct or
union, or which the former is more familiar and usually more useful.
(Chris Torek likes to retroform 'struct' as a humorous acronym for
'STRange spelling for User Constructed Type'.)

<OT> In C++ enum types are also distinct, although they still silently
convert to but not from integers; and all class/struct, union, and
enum tags are automatically usable as typenames without being
explicitly declared as typedefs; but typedefs are still aliases.

And of course class/struct types already have true, builtin,
hierarchical polymorphism. If that's what you want. </>

- David.Thompson1 at worldnet.att.net
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      07-31-2006
Dave Thompson <(E-Mail Removed)> writes:
> On 21 Jul 2006 22:42:50 -0700, "Bill Pursell" <(E-Mail Removed)>
> wrote:
> <snip>
>> I was recently playing around with some similar ideas and
>> had hoped that typedef'ing the void * would generate a
>> compiler warning, but was unable to coerce any warnings. eg

>
> Nope. typedef in C is strictly an alias, not a distinct type.
>
> The only way to create a distinct new type in C is as a struct or
> union, or which the former is more familiar and usually more useful.
> (Chris Torek likes to retroform 'struct' as a humorous acronym for
> 'STRange spelling for User Constructed Type'.)
>
> <OT> In C++ enum types are also distinct, although they still silently
> convert to but not from integers; and all class/struct, union, and
> enum tags are automatically usable as typenames without being
> explicitly declared as typedefs; but typedefs are still aliases.
>
> And of course class/struct types already have true, builtin,
> hierarchical polymorphism. If that's what you want. </>


Actually, enumerated types are distinct in C as well -- but the
enumeration constants are of type int, not of the enum type. For
example:

enum foo { zero, one, two };
enum foo *fp;
int *ip;
ip = fp; /* illegal */

For that matter, a declaration of a pointer, array, or function type
creates a distinct new type. (The language has no facility for
declaring new numeric types.) But structures are the most flexible
form of new types, since they can contain whatever else you need them
to.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-31-2006
Keith Thompson wrote:
> Dave Thompson <(E-Mail Removed)> writes:
>
>>On 21 Jul 2006 22:42:50 -0700, "Bill Pursell" <(E-Mail Removed)>
>>wrote:
>><snip>
>>
>>>I was recently playing around with some similar ideas and
>>>had hoped that typedef'ing the void * would generate a
>>>compiler warning, but was unable to coerce any warnings. eg

>>
>>Nope. typedef in C is strictly an alias, not a distinct type.
>>
>>The only way to create a distinct new type in C is as a struct or
>>union, or which the former is more familiar and usually more useful.
>>(Chris Torek likes to retroform 'struct' as a humorous acronym for
>>'STRange spelling for User Constructed Type'.)
>>
>><OT> In C++ enum types are also distinct, although they still silently
>>convert to but not from integers; and all class/struct, union, and
>>enum tags are automatically usable as typenames without being
>>explicitly declared as typedefs; but typedefs are still aliases.
>>
>>And of course class/struct types already have true, builtin,
>>hierarchical polymorphism. If that's what you want. </>

>
>
> Actually, enumerated types are distinct in C as well -- but the
> enumeration constants are of type int, not of the enum type. For
> example:
>
> enum foo { zero, one, two };
> enum foo *fp;
> int *ip;
> ip = fp; /* illegal */
>

But they are effectively a type in name only in C, by virtue of

enum foo f;

f = 42;

--
Ian Collins.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-31-2006
Ian Collins <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> Dave Thompson <(E-Mail Removed)> writes:

[...]
>> Actually, enumerated types are distinct in C as well -- but the
>> enumeration constants are of type int, not of the enum type. For
>> example:
>>
>> enum foo { zero, one, two };
>> enum foo *fp;
>> int *ip;
>> ip = fp; /* illegal */
>>

> But they are effectively a type in name only in C, by virtue of
>
> enum foo f;
>
> f = 42;


That's merely because there's an implicit conversion between int and
enum foo. C is full of implicit conversions: between void* and any
pointer-to-object type, between any two numeric types, from a null
pointer constant to any pointer type.

A *value* of type enum foo is pretty much interchangeable with any
integer type. An *object* of type enum foo is not. In that sense,
it's a distinct type.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-31-2006
Keith Thompson wrote:
> Ian Collins <(E-Mail Removed)> writes:
>
>>Keith Thompson wrote:
>>
>>>Dave Thompson <(E-Mail Removed)> writes:

>
> [...]
>
>>>Actually, enumerated types are distinct in C as well -- but the
>>>enumeration constants are of type int, not of the enum type. For
>>>example:
>>>
>>> enum foo { zero, one, two };
>>> enum foo *fp;
>>> int *ip;
>>> ip = fp; /* illegal */
>>>

>>
>>But they are effectively a type in name only in C, by virtue of
>>
>>enum foo f;
>>
>>f = 42;

>
>
> That's merely because there's an implicit conversion between int and
> enum foo. C is full of implicit conversions: between void* and any
> pointer-to-object type, between any two numeric types, from a null
> pointer constant to any pointer type.
>
> A *value* of type enum foo is pretty much interchangeable with any
> integer type. An *object* of type enum foo is not. In that sense,
> it's a distinct type.
>

Can you flesh out "An *object* of type enum foo is not" with an example?
I'm not quite sure of the context.

--
Ian Collins.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      07-31-2006
Ian Collins <(E-Mail Removed)> writes:

> Keith Thompson wrote:
>> A *value* of type enum foo is pretty much interchangeable with any
>> integer type. An *object* of type enum foo is not. In that sense,
>> it's a distinct type.
>>

> Can you flesh out "An *object* of type enum foo is not" with an example?
> I'm not quite sure of the context.


If we have:
enum foo { zero, one, two } enum_object;
int int_object;
then &object is not compatible with &int_object. In fact,
enum_object and int_object might not have the same size.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a logic excursion Rainer Weikusat Perl Misc 3 03-22-2013 06:01 PM
Re: A CT&P excursion - beat it up! Ron Ford C Programming 10 06-09-2008 07:46 PM
Re: A CT&P excursion - beat it up! gw7rib@aol.com C Programming 3 06-07-2008 10:01 PM



Advertisments