Simulation of templates in C (for a queue data type)
I' m trying to implement a queue
structure using C. My implementation is very simple; the queue can hold only int
s and nothing else. I was wondering if I could simulate C++
templates in C
(probably by using the preprocessor #define
) so that my queue
can hold any data type.
Note: I do not want to use void*
. I think it is a bit risky and can easily cause bizarre runtime errors.
You can use subtle and ugly tricks in order to create that kind of templates. Here's what I would do:
Creation of a templated list
Macro to define the list
I would first create a macro - let's call it say define_list(type)
- that would create all the functions for a list of a given type. I would then create a global structure containing function pointers to all the list's functions and then have a pointer to that global structure in each instance of the list (note how similar it is to a virtual method table). This kind of thing:
#define define_list(type) \
\
struct _list_##type; \
\
typedef struct \
{ \
int (*is_empty)(const struct _list_##type*); \
size_t (*size)(const struct _list_##type*); \
const type (*front)(const struct _list_##type*); \
void (*push_front)(struct _list_##type*, type); \
} _list_functions_##type; \
\
typedef struct _list_elem_##type \
{ \
type _data; \
struct _list_elem_##type* _next; \
} list_elem_##type; \
\
typedef struct _list_##type \
{ \
size_t _size; \
list_elem_##type* _first; \
list_elem_##type* _last; \
_list_functions_##type* _functions; \
} List_##type; \
\
List_##type* new_list_##type(); \
bool list_is_empty_##type(const List_##type* list); \
size_t list_size_##type(const List_##type* list); \
const type list_front_##type(const List_##type* list); \
void list_push_front_##type(List_##type* list, type elem); \
\
bool list_is_empty_##type(const List_##type* list) \
{ \
return list->_size == 0; \
} \
\
size_t list_size_##type(const List_##type* list) \
{ \
return list->_size; \
} \
\
const type list_front_##type(const List_##type* list) \
{ \
return list->_first->_data; \
} \
\
void list_push_front_##type(List_##type* list, type elem) \
{ \
... \
} \
\
_list_functions_##type _list_funcs_##type = { \
&list_is_empty_##type, \
&list_size_##type, \
&list_front_##type, \
&list_push_front_##type, \
}; \
\
List_##type* new_list_##type() \
{ \
List_##type* res = (List_##type*) malloc(sizeof(List_##type)); \
res->_size = 0; \
res->_first = NULL; \
res->_functions = &_list_funcs_##type; \
return res; \
}
#define List(type) \
List_##type
#define new_list(type) \
new_list_##type()
Generic interface
Here are some macros that simply call the list's functions via the stored function pointers:
#define is_empty(collection) \
collection->_functions->is_empty(collection)
#define size(collection) \
collection->_functions->size(collection)
#define front(collection) \
collection->_functions->front(collection)
#define push_front(collection, elem) \
collection->_functions->push_front(collection, elem)
Note that if you use the same structure to design other collections than lists, you'll be able to use the last functions for any collections that stores the good pointers.
Example of use
And to conclude, a small example of how to use our new list template:
/* Define the data structures you need */
define_list(int)
define_list(float)
int main()
{
List(int)* a = new_list(int);
List(float)* b = new_list(float);
push_front(a, 5);
push_front(b, 5.2);
}
You can use that amount of tricks if you really want to have some kind of templates in C, but that's rather ugly (just use C++, it'll be simpler). The only overhead will be one more pointer per instance of data structure, and thus one more indirection whenever you call a function (no cast is done, you don't have to store void*
pointers, yeah \o/). Hope you won't ever use that :p
Limitations
There are of course some limitations since we are using mere text replacement macros, and not real templates.
Define once
You can only define each type once per compile unit, otherwise, your program will fail to compile. This can be a major drawback for example if you write a library and some of your headers contain some define_
instructions.
Multi-word types
If you want to create a List
whose template type is made of several words (signed char
, unsigned long
, const bar
, struct foo
...) or whose template type is a pointer (char*
, void*
...), you will have to typedef
that type first.
define_list(int) /* OK */
define_list(char*) /* Error: pointer */
define_list(unsigned long) /* Error: several words */
typedef char* char_ptr;
typedef unsigned long ulong;
define_list(char_ptr) /* OK */
define_list(ulong) /* OK */
You will have to resort to the same trick if you want to create nested lists.
Well, the only possibilty that comes to my mind are macros (#define
s). Maybe something like:
queue.h:
#define TYPE int
#define TYPED_NAME(x) int_##x
#include "queue_impl.h"
#undef TYPE
#undef TYPED_NAME
#define TYPE float
#define TYPED_NAME(x) float_##x
#include "queue_impl.h"
#undef TYPE
#undef TYPED_NAME
...
queue_impl.h:
//no include guard, of course
typedef struct
{
TYPE *data;
...
} TYPED_NAME(queue);
void TYPED_NAME(queue_insert) (TYPED_NAME(queue) *queue, TYPE data)
{
...
}
If it works (which I'm not 100% sure of, being not such a preprocessor expert), it should give you the structs int_queue
and float_queue
, along with the functions
void int_queue_insert(int_queue *queue, int data);
void float_queue_insert(float_queue *queue, float data);
Of course you will have to do the instantiation of the "template" yourself for all the types you need, but this amounts to repeating the 5-line block in queue.h
. The actual implementation has to be written only once. Of course you can refine this even more, but the basic idea should be clear.
This will at least give you perfectly type-safe queue templates, though lacking the convenience of completely matching interfaces (the functions have to carry the type name, since C doesn't support overloaded functions).
Implement a queue containing void* data, and interpret this void* as pointer to any type, or even primitive type like int.
Using #define is possible, but think about debugging, if something is wrong...