"templates" in old-school C?

Started by
17 comments, last by smitty1276 20 years, 2 months ago
It''s hard to search for this topic, because a lot of sites concerning C++ templates use the word C... sorry. If I want to do something as (seemingly) simple as writing a reusable linked list implementation, how would I go about it in C? I realize that I could just use void* for all of my nodes, and just cast them, but is there some slick way using typedefs and macros, etc., to allow for an unknown type that can be determined at runtime?
Advertisement
Don''t triple post. Delete the other ones.

No, void* is the way to do it. C has nothing really in the ways of tricks like C++ has.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
I didn''t see a delete button, but I saw it under edit (finally)... I deleted them, but I think they''re still there.

Sorry.
What the hell, I''ll bite.

First off, unlike templates in C++, your system will be evaluated at runtime (templates are evaluated at compile time). The system will actually have a lot in common with implementing OO in C (so you might as well do it, too). Consider the following C++ fragment:
template < typename T >void SomeFunction( const T & t ){  t.generic_method();}
This application of templates requires a means for determining that the actual type instantiating the template (T) has the method generic_method. This is trivial in C++ because the code is fully expanded and then passed to the compiler, which simply looks into the symbol table. Since your system would be evaluated at runtime, you''d have to store an explicit symbol table which would be searched.

Furthermore, since neither C nor C++ is introspective (there is no way to programmatically query an entity as to its properties), you''ll need to use specific tokens as associative indices to indicate the existence of a certain function in an object''s interface. This could simply be the function name (an entry with the key "find" indicates the existence of a function find()).

C is not inherently object-oriented and doesn''t support inheritance, so all your objects must actually be of one type in C, but masquerade as being of different types. This is the hard part. You need to store a generic reference to all your objects (void *), but supply a conversion mechanism to recover the original object complete with type. This suggests typecasting, and would be easy if the function-style typecast could be assigned to a function pointer. Unfortunately it can''t, AFAICT.

This is such an intrinsic problem that I am unable to think of a workaround or solution. So you''re SOL.

Wasn''t that fun!
I think you could probably use a macro to generate your class with whatever types you want, but wouldn''t using C++ be easier?
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I'm actually being forced to take C programming at school, only because it is a prerequisite for all of the other computer science classes I already have that are transferring (CSI, CSII, CSIII, etc...) You'd think since I have the classes that it is a prereq for... nevermind. In any case, I talked them into at least letting me skip to *ADVANCED* C, which is still a joke.

We are supposedly covering linked lists this week, and since I have already known how to do those for 10 years or so, I figured I would try to spice things up just for fun.

My point is, I CAN'T USE C++... I just wanted something "fun".



[edited by - smitty1276 on February 16, 2004 10:19:27 PM]
If you don''t mind ugly...

//of course you know templated functions:#define Add(A,B) (A + B)//similarly, a templated class:#define ListNode(Type) \struct \{ \    Type  Value; \    void* Prev; \    void* Next; \} void main ( ){    ListNode(int) IntNode;    ListNode(float) FloatNode;     IntNode.Value = 4;    FloatNode.Value = 0.5;}

Cool, huh? Of course you can have trouble doing certain things... but it works. Only thing is you can''t name the struct, hence the void* pointers it uses. The reason you can''t name it is you can''t create different names for the different types, so you''d have multiple declarations of the same structure.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Post screenshots of your projects. There''s already 134 screenshot posts.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
you could, i guess, abuse __LINE__ :D for unique names, that is:D



If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

I tried the define thing, and I kept getting weird errors... maybe I''ll try again. It was just a quick attempt and I didn''t really do any trouble shooting.

Thanks for the responses.
why do you have to do this anyway? i could go the "c sucks" route, but ill be nice. however, you can also do hybrid (mostly c and toss in the c++ features you like)
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)

This topic is closed to new replies.

Advertisement